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); 
}
6
  • 2
    If you want fire+forget, then, and only then, async void is fine. You forgot to put the async modifier though. Commented Jul 19, 2013 at 6: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. Commented Jul 19, 2013 at 7: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. Commented Jul 19, 2013 at 7: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. Commented Jul 19, 2013 at 7:34
  • Ye, that's normal for fire+forget. Commented Jul 19, 2013 at 7:44

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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);
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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.