Suppose I have a simple C# console application:
class Program { static async void func() { Thread.CurrentThread.Name = "main"; await Task.Run(() => { Thread.CurrentThread.Name = "child"; Thread.Sleep(5000); }); Console.WriteLine("continuation is running on {0} thread", Thread.CurrentThread.Name); } static void Main(string[] args) { func(); Thread.Sleep(10000); } }
When the skip is 5000 ms, we see the message "Continuation works on the child thread." When another 5000 ms passes, the main thread exits and the application closes. This looks pretty logical: the asynchronous task and its continuation are executed on one child thread.
But suppose I have a simple WPF application:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } async private void mainWnd_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Thread.CurrentThread.Name = "main"; await Task.Run(() => { Thread.CurrentThread.Name = "child"; Thread.Sleep(5000); }); this.Title = string.Format("continuation is running on {0} thread", Thread.CurrentThread.Name); } private void mainWnd_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { Thread.Sleep(10000); } }
Now, when we press the left mouse button and skip 5000 ms, we see that the heading “continue working on the main thread”. In addition, if we press the left button and then the right button, the application will first start the mainWnd_MouseLeftButtonDown handler, then the mainWnd_MouseRightButtonDown handler (in the main thread), the main thread will sleep for 10,000 ms, and then continue the asynchronous task from mainWnd_MouseLeftButtonDown to continue to be executed flow.
Why is the async-await mechanism different for these two situations?
I know that the WPF method can be explicitly launched in the user interface thread through Dispatcher.Invoke , but the async-await mechanism is not WPF specific, so its behavior should be equal in any kind of application, shouldn it?
Any help would be appreciated.