Async ShowDialog

It’s easy to implement with Task.Yield, like below (WinForms, no exception handling for simplicity). It’s important to understand how the execution flow jumps over to a new nested message loop here (that of the modal dialog) and then goes back to the original message loop (that’s what await progressFormTask is for): namespace WinFormsApp { internal … Read more

A pattern to pause/resume an async task?

Updated for 2019, I’ve recently had a chance to revisit this code, below is complete example as a console app (warning: PauseTokenSource needs good unit testing). Note, in my case, the requirement was that when the consumer-side code (which requested the pause) would continue, the producer-side code should have already reached the paused state. Thus, … Read more

Is async/await suitable for methods that are both IO and CPU bound?

There are two good answers already, but to add my 0.02… If you’re talking about consuming asynchronous operations, async/await works excellently for both I/O-bound and CPU-bound. I think the MSDN docs do have a slight slant towards producing asynchronous operations, in which case you do want to use TaskCompletionSource (or similar) for I/O-bound and Task.Run … Read more

ASP.NET Controller: An asynchronous module or handler completed while an asynchronous operation was still pending

In Async Void, ASP.Net, and Count of Outstanding Operations, Stephan Cleary explains the root of this error: Historically, ASP.NET has supported clean asynchronous operations since .NET 2.0 via the Event-based Asynchronous Pattern (EAP), in which asynchronous components notify the SynchronizationContext of their starting and completing. What is happening is that you’re firing DownloadAsync inside your … Read more

Use Task.Run() in synchronous method to avoid deadlock waiting on async method?

It seems you understand the risks involved in your question so I’ll skip the lecture. To answer your actual question: Yes, you can just use Task.Run to offload that work to a ThreadPool thread which doesn’t have a SynchronizationContext and so there’s no real risk for a deadlock. However, using another thread just because it … Read more

How can I prevent synchronous continuations on a Task?

New in .NET 4.6: .NET 4.6 contains a new TaskCreationOptions: RunContinuationsAsynchronously. Since you’re willing to use Reflection to access private fields… You can mark the TCS’s Task with the TASK_STATE_THREAD_WAS_ABORTED flag, which would cause all continuations not to be inlined. const int TASK_STATE_THREAD_WAS_ABORTED = 134217728; var stateField = typeof(Task).GetField(“m_stateFlags”, BindingFlags.NonPublic | BindingFlags.Instance); stateField.SetValue(task, (int) stateField.GetValue(task) … Read more

Await in catch block

Update: C# 6.0 supports await in catch Old Answer: You can rewrite that code to move the await from the catch block using a flag: WebClient wc = new WebClient(); string result = null; bool downloadSucceeded; try { result = await wc.DownloadStringTaskAsync( new Uri( “http://badurl” ) ); downloadSucceeded = true; } catch { downloadSucceeded = … Read more