80

I have the following code:

public static async Task<string> Start(IProgress<ProcessTaskAsyncExProgress> progress)
{
    const int total = 10;
    for (var i = 0; i <= total; i++)
    {
        await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture)));
        if (progress != null)
        {
            var args = new ProcessTaskAsyncExProgress
            {
                ProgressPercentage = (int)(i / (double)total * 100.0),
                Text = "processing " + i
            };
            progress.Report(args);
        }
    }
    return "Done";
}

private static string RunLongTask(string taskName)
{
    Task.Delay(300);
    return taskName + "Completed!";
}

How do I get back the string value of RunLongTask from this line: await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture)));?

I've tried:

var val = await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture))).Result;

But I get an error saying "string is not awaitable".

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Null Reference
  • 11,260
  • 40
  • 107
  • 184

3 Answers3

123

Remove the Result from the end. When you await you will get the Result back from the await-able method.

var val = await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture)));
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
21

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<string> 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;
Darren
  • 9,014
  • 2
  • 39
  • 50
  • 5
    Using .Result is not recommended, [as it can cause deadlocks](https://stackoverflow.com/questions/15021304/an-async-await-example-that-causes-a-deadlock). – Gabriel Morin May 14 '19 at 21:02
  • 1
    @GabrielMorin Yes "Normally" you shouldn't do this, like 99% of the time. – Darren Nov 22 '19 at 05:56
-2

The virtual function allows you to override the function in the derived class. The task are run one at a time sequentially. The Task.CompletedTask signals the function has completed.

public class BaseTest
{
    public virtual Task LoadDataAsync(ITestOutputHelper output) {
        for (int i = 0; i < Math.Pow(10, 5); i++)
        {
            output.WriteLine("Base {0}", i);
        }
        return Task.CompletedTask; 
    }
}
public class DerivedTest : BaseTest
{
    public override Task LoadDataAsync(ITestOutputHelper output)
    {
        for (int i = 0; i < Math.Pow(10, 6); i++)
        {
            output.WriteLine("Derived {0}", i);
        }
        return Task.CompletedTask;
    }
}


 [Fact]
    public async Task TestVirtualTask()
    {
        BaseTest parent_obj = new BaseTest();
        DerivedTest obj = new DerivedTest();
        List<Task> tasks = new List<Task>();
        tasks.Add(obj.LoadDataAsync(output));
        tasks.Add(parent_obj.LoadDataAsync(output));

        Task.WaitAll(tasks.ToArray());
        Assert.True(true);
    }

You can add the async function and await a task.run. This adds verbage without increasing functionality.

      public virtual async Task LoadDataAsync(ITestOutputHelper output) {
await Task.Run(() =>
            {
                for (int i = 0; i < Math.Pow(10, 6); i++)
                {
                    output.WriteLine("Derived {0}", i);
                }
                return;
            });
 }
Golden Lion
  • 3,840
  • 2
  • 26
  • 35