6

I have the following scenario:

async void DoStuff() { // ... } button1.Click += (s, p) => { DoStuff(); }; 

I'm not sure what happens when I call an async void method while the first call is still incomplete. Will the call create a new thread everytime it is called or will the call destroy and override the previous thread created? Also, will the local method variables be shared if the former assumption is right?

EDITED:

I had misunderstood the async await thing because in windows 8 apps, it behaves differently. If you thought that calling an async method was the same as creating a new thread like me, please read this clarifying aricle.

0

2 Answers 2

8

I'm not sure what happens when I call an async void method more than once at the same time

You mean if another call occurs while the first call is still incomplete (potentially "paused")? It will work just fine. There's no "overriding", no "destroying". You'll end up with another instance of the state machine (which is what the compiler builds for you).

The two state machines will have entirely separate local variables etc. Of course, only one of them can actually run at a time, unless somewhere in the async method you avoid capturing the context.

Don't forget that starting an async method doesn't start a new thread. Some async operations you start within the async method may start new threads, but that's a different matter. Nothing within the generated code creates a new thread.

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

2 Comments

I'm watching your video on pluralsight right now and I STILL don't get it. I understand that the awaiter thing may return finished right away and that, in that case, nothing asynchronous happens. But if there is actual work to be done, and the UI thread receives a continuation, then by default does that mean that the UI thread executes the continuation whenever it's not busy (such that nothing asynchronous happens at all)?
@dudeNumber4: No, the continuation is only executed when the task has completed... but it's executed in the UI thread. So yes, it needs to be when the UI thread isn't busy doing something else, but it's not "whenever" it's not busy. Note that the UI thread should spend most of its time being not busy.
6

No.

Async methods have nothing to do with threads.
Rather, an async method will execute its code on the caller's thread, exactly like a regular method call, until the first await.

The code after each await will run on the thread the the awaitable ran its callback on.
This depends on exactly what you're awaiting.
Typically, you'll be awaiting Task objects, which by default run their callbacks on the UI thread.

3 Comments

I mean while the first call is still incomplete
@DanielSan: And therefore what? Async methods still have nothing to do with threads. Are you asking how a specific asynchronous operation that you call inside the method behaves?
The continuations only run on the UI thread if the former part was on the UI thread. If the UI thread is busy, the continuation can't execute until the UI thread returns to the main message pump.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.