4

When using async Task method it is required to place await before method. I need code to be executed in non UI blocking manner and don't want to await. My only idea is to use:

private void TaskFactory()
{ 
    CancellationTokenSource token_TaskFactory = new CancellationTokenSource();
    ParallelOptions parOpts = new ParallelOptions();
    parOpts.CancellationToken = token_TaskFactory.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    TaskCreationOptions atp = new TaskCreationOptions();     
    atp = TaskCreationOptions.PreferFairness;
    Task TaskFactory = Task.Factory.StartNew(() => 
    {
       if (!token_TaskFactory.IsCancellationRequested)     
       {
         Thread.Sleep(5000);
       }
       else
       {

       }
    }, token_TaskFactory.Token, atp, TaskScheduler.Default); 
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
as74
  • 700
  • 4
  • 12
  • 25
  • 2
    If you want fire+forget, then, **and only then**, `async void` is fine. You forgot to put the `async` modifier though. – It'sNotALie. Jul 19 '13 at 06:47
  • Where I forgot to put async modifier? In void TaskFactory() I don't want async to not use await then. Method doesn't block UI. – as74 Jul 19 '13 at 07:14
  • 1
    You have to put `async` in the method declaration, and you have to use `await` in the method, but you will not need to `await` the method itself. – It'sNotALie. Jul 19 '13 at 07:20
  • When I don't await the method itself I get warning: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. I wanted to prevent this type of warning. Unless I should not worry when want to use fire and forget method. – as74 Jul 19 '13 at 07:34
  • Ye, that's normal for fire+forget. – It'sNotALie. Jul 19 '13 at 07:44
  • "you will not need to await the method itself" I would mark this as answer. Thank you for your effort Soner. – as74 Jul 21 '13 at 05:02

1 Answers1

8

When using async Task method it is required to place await before method.

The correct way to handle this is to await the method, and make the calling method async Task or async Task<TResult>. This will have a cascading effect as async travels up through your code.

In a UI application, you will usually end up at an event handler, which cannot be async Task, and at that point you can make the event handler async void.

If you cheat this process by making a regular method async void, you cause other problems, particularly around error handling and composability (i.e. unit testing). See my MSDN article or one of the many talks about why async void should be avoided.

I need code to be executed in non UI blocking manner and don't want to await.

Why don't you "want" to use await? You have asynchronous code that you need to run in a non-blocking fashion, and that's exactly what await does!

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 1
    I don't want to use await because need to execute a few async methods parallelly without waiting one for other. Your MSDN article is priceless. Thanks Stephen. – as74 Jul 21 '13 at 05:11
  • 7
    In that case, you just start the tasks like `Task first = Task.Run(...); Task second = Task.Run(...);` and then `await` them all at once like `await Task.WhenAll(first, second);` – Stephen Cleary Jul 21 '13 at 11:24
  • What if you do not care if it blocks for a bit but the only method available to you is async ? It seems that would be the perfect scenario where you would call the async method without await. – Bill Greer Sep 04 '15 at 01:37
  • 2
    @BillGreer: I'm not understanding your question. If the method is async, it (generally) won't block. Not using await is a kind of "fire and forget", where you don't care *when* the method completes, what the *result* is, or whether it threw an *exception*. All three of those concerns taken together are almost never true; your code usually cares about at least one of them. – Stephen Cleary Sep 04 '15 at 12:31