繁体   English   中英

任务<t>和 Func 委托中的任务</t>

[英]Task<T> and Task in Func delegate

我想清理我的一些代码。 我有重载方法。 我能以某种方式简化这段代码并在另一个方法中调用一个方法吗? 无法弄清楚如何做到这一点。

private async Task<T> DecorateWithWaitScreen<T>(Func<Task<T>> action)
{
    SplashScreenManager.ShowForm(this, typeof(WaitForm), true, true, false);
    try
    {
        return await action();
        
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
        throw;
    }
    finally
    {
        SplashScreenManager.CloseForm(false);
    }
}

private async Task DecorateWithWaitScreen(Func<Task> action)
{
    SplashScreenManager.ShowForm(this, typeof(WaitForm), true, true, false);
    try
    {
        await action();
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
        throw;
    }
    finally
    {
        SplashScreenManager.CloseForm(false);
    }
}

怎么样:

private Task DecorateWithWaitScreen(Func<Task> action)
    => DecorateWithWaitScreen<int>(async () => { await action(); return 0; });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM