Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task<T> and Task in Func delegate

I want to cleanup some of my code. I have overloaded method. Can I somehow simplyfy this code and invoke one method in another ? Cant figure out how to do this.

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);
    }
}
like image 776
TjDillashaw Avatar asked Mar 09 '23 15:03

TjDillashaw


1 Answers

How about:

private Task DecorateWithWaitScreen(Func<Task> action)
    => DecorateWithWaitScreen<int>(async () => { await action(); return 0; });
like image 168
Marc Gravell Avatar answered Mar 21 '23 00:03

Marc Gravell