Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I thought one of the points about async/await is that when the task completes, the continuation is run on the same context when the await was called, which would, in my case, be the UI thread.

So for example:

System.Diagnostics.Debug.WriteLine("2: Thread ID: " + Thread.CurrentThread.ManagedThreadId);
await fs.ReadAsync(data, 0, (int)fs.Length);
System.Diagnostics.Debug.WriteLine("3: Thread ID: " + Thread.CurrentThread.ManagedThreadId);


I would NOT expect this:

2: Thread ID: 10
3: Thread ID: 11


What gives? Why is the thread ID for the continuation different than the UI thread?

According to this article[^] I would need to explicitly call ConfigureAwait to change the behavior of the continuation context!

Marc
Posted
Comments
Sergey Alexandrovich Kryukov 17-Feb-14 16:50pm    
Marc, why would you use any asynchronous API at all, if you are already using threads? I really don't think it makes any practical sense. Asynchronous API were somewhat popular before threading became a commonplace (not with me though :-), but threading implementations are more straightforward. Moreover, you use the same thread synchronization techniques over and over, while with asynchronous API it always depends on API detail. I see no need in asynchronous APIs...
—SA
Marc Clifton 18-Feb-14 9:39am    
await and async are keywords, not API (at least, in my definition of the concepts) that set up continuation passing when the task is complete. Yes, you can use them with Task<>, but one of the neat things about await is that it marshals onto the context of the caller, which for my purposes is the UI thread, so no more BeginInvoke calls! Also, according the MSDN docs for asynchronous file I/O, they suggest using the new Async functions in conjunction with await/async.
Sergey Alexandrovich Kryukov 18-Feb-14 11:18am    
I got it, thank you for the note. Maybe it makes sense, but Microsoft recommendations never impressed me, until I get to the facts. I would be careful about await, even though I understand its neatness. For a record, I was talking about threads, not Task; they are very different things.
—SA

1 solution

Turns out I was calling the async read before the UI was up and running, so there was no continuation context!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900