Getting return value from Task.Run

前端 未结 2 1004
迷失自我
迷失自我 2020-12-29 00:44

I have the following code:

public static async Task Start(IProgress progress)
{
    const int total = 10;
            


        
2条回答
  •  鱼传尺愫
    2020-12-29 01:38

    This is not a direct answer to old question, but for others searching:

    "Normally" you shouldn't do this, but sometimes you need to match a library API so you can use a wrapper function like below:

    private async Task WrapSomeMethod(string someParam)
    {
        //adding .ConfigureAwait(false) may NOT be what you want but google it.
        return await Task.Run(() => SomeObj.SomeMethodAsync(someParam)).ConfigureAwait(false);
    }
    

    And then call that instead with .Result like below:

    string blah = WrapSomeMethod(someParam).Result;
    

提交回复
热议问题