0

I have the following code:

    public static async void PopulateMetrics()
    {
        await Task.Run(() =>
        {
            if (App.CPUSpeed == 0)
            {
                var stopWatch = Stopwatch.StartNew();
                stopWatch.Start();
                ArrayList al = new ArrayList(); for (int i = 0; i < 5000000; i++) al.Add("hello");
                App.CPUSpeed = 20000 / stopWatch.ElapsedMilliseconds;
            }
        });

    }

The IDE is telling me that the async method should not return void. Would making it return Task<bool> and returning true fix this but is that needed?

Also would there be any difference in calling this between:

_ = PopulateMetrics()

and

await PopulateMetrics()

Here is what I have for the calling method. Note that for all except the PopulateMetrics, I have exception handling in each of the async methods.

        if (Connectivity.NetworkAccess == NetworkAccess.Internet)
        {
            if (Settings.Rev == REV.No && (new[] { 15, 30, 50 }).Contains(Settings.Trk2))
            {
                _ = ReviewAppAsync(Settings.Trk2);
            }
            if (App.devIsPhysical && (new[] { 10, 20, 30 }).Contains(Settings.Trk2))
            {
                _ = CheckLatestVersion();
            }
            _ = Helper.PopulateMetrics();
            _ = Helper.LogStart();
        }
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • async void should only be used in event handlers. Otherwise this approach is prone to evoke dead locks! Because you are mixing synchronous and asynchronous processing – Mong Zhu Feb 21 '20 at 16:01

3 Answers3

5

As a rule, when a non-async method returns void, its async counterpart should return Task:

public static async Task PopulateMetrics()

According to Microsoft, you should use void return from async methods only when you implement an event handler:

Void-returning async methods have a specific purpose: to make asynchronous event handlers possible. It is possible to have an event handler that returns some actual type, but that doesn't work well with the language; invoking an event handler that returns a type is very awkward, and the notion of an event handler actually returning something doesn't make much sense. Event handlers naturally return void, so async methods return void so that you can have an asynchronous event handler.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I changed this to Task and the compiler does not give a warning. Is that because await Task.Run returns a Task? How about the way it's being called. _= or await. Is there any difference? – Alan2 Feb 21 '20 at 16:02
  • 1
    @Alan2 When you do `_ = FooAsync()` without an `await`, the call becomes "call-and-forget". Typically, you want to `await` your call at some point, either directly or by calling `await` on `Task.WhenAny`/`Task.WhenAll` with the `Task` from the async call passed as parameter. – Sergey Kalinichenko Feb 21 '20 at 16:05
1

1) Normally, you would want to return a Task. The main exception should be when you need to have a void return type (for events). If there's no reason to disallow having the caller await your task, why disallow it?

2) async methods that return void are special in another aspect: they represent top-level async operations, and have additional rules that come into play when your task returns an exception. The easiest way is to show the difference is with an example:

    static async void f()
    {
        await h();
    }

    static async Task g()
    {
        await h();
    }

    static async Task h()
    {
        throw new NotImplementedException();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        g();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        GC.Collect();
    }

f's exception is always "observed". An exception that leaves a top-level asynchronous method is simply treated like any other unhandled exception. g's exception is never observed. When the garbage collector comes to clean up the task, it sees that the task resulted in an exception, and nobody handled the exception. When that happens, the TaskScheduler.UnobservedTaskException handler runs. You should never let this happen. To use your example,

    public static async void AsyncMethod2(int num)
    {
        await Task.Factory.StartNew(() => Thread.Sleep(num));
    }

Yes, use async and await here, they make sure your method still works correctly if an exception is thrown.

for more information see: http://msdn.microsoft.com/en-us/magazine/jj991977.aspx

  • For the calling method is _ = xx same as await xx if I have exception handling in each of those methods? – Alan2 Feb 21 '20 at 16:06
0

Just set the return type to Task to get rid of the IDE warning:

 public static async Task PopulateMetrics()
    {
        await Task.Run(() =>
        {
            if (App.CPUSpeed == 0)
            {
                var stopWatch = Stopwatch.StartNew();
                stopWatch.Start();
                ArrayList al = new ArrayList(); for (int i = 0; i < 5000000; i++) al.Add("hello");
                App.CPUSpeed = 20000 / stopWatch.ElapsedMilliseconds;
            }
        });
    }

Regarding the differences in the two calls, you must think that calling an async method is like starting a new Thread: the current thread is not blocked until the task is awaited or is asked for its's result. Consider this code:

class Program
{
   static async Task Main() // Compiler will warn you 'cause you're not awaiting!
   {
        _ = PopulateMetrics();

       // do something else
   }
}

Chances are the program will exit before the PopulateMetrics task has finished. Since PopulateMetrics returns async Task, you should do:

class Program
{
   static async Task Main() 
   {
       var popMetricsTask = PopulateMetrics();

       // do something else

       popMetricsTask.Wait(); // or: await popMetricsTask;
   }
}

To be sure your task has finished before exiting program.

Claudio Valerio
  • 2,302
  • 14
  • 24