50

Consider Using async without await.

think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously.

I want to write a method that should run async but don't need use await. For example when use a thread

public async Task PushCallAsync(CallNotificationInfo callNotificationInfo)
{
    Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
}

I want call PushCallAsync and run async and don't want to use await.

Can I use async without await in C#?

11
  • 2
    Well. It's all up to what your design issue is, here. What do you expect "async" to do? Commented Jul 23, 2013 at 9:15
  • 6
    If you're not going to await this method why bother declaring it async at all? The text that you quote sums it up pretty nicely IMO. Commented Jul 23, 2013 at 9:17
  • 1
    I think you just use Task.Run directly. async/await isn't really a multithreading mechanism, in fact I think the runtime executes things on as few threads as possible. It's mostly about the compiler automatically transform your code into continuation-passing style so you only wait for results (or completion) of a background operation when absolutely necessary. When you don't need to wait (or await) for the results of your call, this is not the language feature you're looking for. Commented Jul 23, 2013 at 9:28
  • 4
    @millimoose: The way async interacts with threads is a bit more complex, and the default behavior can be easily overridden. async is not a multithreading mechanism, nor does it always run on a single thread. I have a blog post that summarizes how async schedules its continuations. Commented Jul 23, 2013 at 15:41
  • 2
    @millimoose: Not an implementation detail. It is clearly specified - and has to be, so its behavior is always predictable and reliable (once you understand the mechanism). Commented Jul 23, 2013 at 18:58

5 Answers 5

45

You still are misunderstanding async. The async keyword does not mean "run on another thread".

To push some code onto another thread, you need to do it explicitly, e.g., Task.Run:

await Task.Run(() => Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId));

I have an async/await intro post that you may find helpful.

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

7 Comments

is Task.Run can be not awaited?
@toha: I don't understand your question. The Task.Run is awaited.
I use task, and not await. but sometimes it is not called until completed.. when out of the function block.. I hope the task running until complete even main process out of function block. can it be done? If I use await it is takes long time so I need It is not awaited and called until finished even out of function call
@toha: It's not really clear what you're asking. Can you ask your own question?
No the question is same.. I am not await my async task. but I use it on Exception Block and the async Task function is not completely finish when awaiting another function call I think this is about async task life cycle when fired not await and on exception block.. do you have article about that Sir?
|
37

If your Logger.LogInfo is already async this is enough:

public void PushCallAsync(CallNotificationInfo callNotificationInfo)
{
    Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
}

If it is not just start it async without waiting for it

public void PushCallAsync(CallNotificationInfo callNotificationInfo)
{
    Task.Run(() => Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId));
}

7 Comments

I read it, seems to me more like a style difference than an actual difference in bahavior, isn't it?
No. Task.Run uses different defaults: DenyChildAttach and TaskScheduler.Default. The TaskScheduler is particularly important because StartNew will use TaskScheduler.Current by default, which makes it schedule the delegate differently based on the context of the caller. This has tripped up so many people that many teams have adopted code rules that never allow StartNew unless the TaskScheduler is specified.
Didn't know that, thx for the info. Changed the code to Task.Run
By convention you should not name a method "Async" unless it has the "async" keyword.
@GraemeWicksted actually the Async suffix should be used for methods that return a Task or ValueTask. Whether the method is implemented with the async keyword is irrelevant.
You're correct.
|
6

You're misunderstanding async. It actually just tells the Compiler to propagate the inversion of control flow it does in the background for you. So that the whole method stack is marked as async.

What you actually want to do depends on your problem. (Let's consider your call Logger.LogInfo(..) is an async method as it does eventually call File.WriteAsync() or so.

  • If you calling function is a void event handler, you're good. The async call will happen to some degree (namely File.WriteAsync) in the background. You do not expect any result in your control flow. That is fire and forget.
  • If however you want to do anything with the result or if you want to continue only then, when Logger.LogInfo(..) is done, you have to do precautions. This is the case when your method is somehow in the middle of the call-stack. Then Logger.LogInfo(..) will usually return a Task and that you can wait on. But beware of calling task.Wait() because it will dead lock your GUI-Thread. Instead use await or return the Task (then you can omit async):

public void PushCallAsync(CallNotificationInfo callNotificationInfo) 
{
   return Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId); 
}

or

 public async void PushCallAsync(CallNotificationInfo callNotificationInfo) 
 {
    await Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId); 
 }

3 Comments

You should not use async void unless it is an event handler. That is the only acceptable time and is the only correct way of making an async event handler. If your synchronous method is void, it should be async Task. This is due to exception handling where event handlers will throw on the SynchronizationContext (similarly to synchronous events) rather than the caller. If possible the async event handler should be a thin private wrapper an async Task function (so it can be tested or re-used).
@GraemeWicksted you are certainly right and I support 'do not do async void' but for the understanding it is what I need - and eventually you'll usually have async void somewhere
I'm not sure I understand your reply. How is it justified here? Looks like it should be async Task to me as it is not an event handler. Remember: async Task is essentially async Task<void> but the latter simply does not exist. The only difference between async void and async Task is exception propagation and it only makes sense on event handlers. I have not observed a concrete example that would indicate otherwise.
3

Given your example

public async Task PushCallAsync(CallNotificationInfo callNotificationInfo)
{
    Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
}
  • Logger.LogInfo is called synchronously
  • The async keyword gives the method PushCallAsync the capability to await, but it never awaits anything

If you intend to make the method run asynchronously - as the name PushCallAsync implies -, you have to find an alternative to synchronously calling LogInfo.

If a LogInfoAsync method exists, trying to evade using await is ill-advised. Await is important because:

  • It captures and throws exceptions that may occur on the task execution - which would otherwise be lost / unhandled
  • It ensures execution order by waiting for the result

If you specifically want a fire-and-forget behavior, where you do not depend on execution order (e.g. in this case you don't care about the order of the log messages), you call LogInfoAsync() without awaiting the result.

Since you don't use any await you do not mark the method async. What makes it asynchronous is not the async keyword but it calling other methods asynchronously.

public Task PushCallAsync(CallNotificationInfo callNotificationInfo)
{
    // Fire and forget - we do not care about the result, failure, or order of this log message
    _ = Logger.LogInfoAsync("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
    Task.CompletedTask;
}

or non-async

public void PushCall(CallNotificationInfo callNotificationInfo)
{
    // Fire and forget - we do not care about the result, failure, or order of this log message
    _ = Logger.LogInfoAsync("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
}

Note that the method name Push implies it is in order. So I would name it differently if you don't care about order. Otherwise, as Push implies, using await would be correct to ensure order.

Comments

3

If Logger.LogInfo is a synchronous method, the whole call will be synchronous anyway. If all you want to do is to execute the code in a separate thread, async is not the tool for the job. Try with thread pool instead:

ThreadPool.QueueUserWorkItem(foo => PushCallAsync(callNotificationInfo));

5 Comments

Favour the Parallel Task library, rather than calling the threadpool directly... this can have rather unforeseen consequences depending on the platform (WinRT, Win, WP8, etc). In my experience. =)
You got me interested... Could you please provide more details on what the consequences could be?
Well. I've managed to completely choke the CPU on several devices, using QueueUserWorkItem (using only two threads, mind you, so it's touchy as well), whereas the Task library will manage it perfectly. I've also had an issue where I've queued several threads, and none reaches the callback until they're all done, in a sort of thread-avalanche.
If Logger.LogInfo is not a synchronous method don't you mnean If Logger.LogInfo is a synchronous method??

Your Answer

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