, .
MessageBox.Show . ThreadContext, , , , - , . Show , , .MessageBox Form - user32.dll , .ThreadPool.QueueUserWorkItem. , ( -). , , , .NET. : . , , WindowsFormsSynchronizationContext, , Windows.- , .
- .
MessageBox.Show .
await (await Task.Delay(1000);), - "!" ., "!" . ?! ?!
, "!" "!". , . , - " " .
, . , , . await, "!". , () .
Thread.Sleep(1000); await, . , Thread.Sleep "!". , , "!" , "!" , , "!" .
Windows. "" . , , WM ( "set focus" ).
await , . , , , , - MessageBox ( .NET Form!), - , "!" . .
, , - , - MessageBox.Show("Exit!");, .
:
. , , - , . , , .
, ""! , - "!" , - ( ), ""! .
, MessageBox.Show("Exit!"); ( Application.EnterThreadModal). WinAPI (user32.dll) MessageBox, . WM, Invoke - "!" . . Message.Show, .
, . -, MessageBox.Show, , .
In fact, you would run your task outside of the user interface thread, and only a continuation requiring access to the user interface would be in the user interface thread. However, it is very interesting how it MessageBoxcaptures what is happening - it can have unpleasant consequences if your background task gets stuck, causing something in the user interface thread, which is effectively transmitted in the message window; your asynchrony goes there :))