My understanding is that in a windows forms application if the UI thread throws an exception and it is not handled by user code the application will crash in a manner that is obvious to the user. (A message will appear on the screen indicating that the application has crashed, or the window will freeze, or both)
Now consider the following code:
try
{
PerformAsyncOperation();
while(!async_op_complete)
{
Application.DoEvents();
}
}
catch
{
Print("exception");
}
My undestanding is that because a windows form has a synchronization context, the async operation (and I'm not talking about .net 4.5, by the way) won't be performed until control leaves the event handler procedure. Once that happens the async code will run, inside the UI thread. If that code were to throw an exception then it wouldn't be caught by the try-catch statement surrounding the call to the async method, since control will have already left that try-catch statement by the time the exception is thrown.
But in cases such as the one above, the fact that we are looping means that control remains inside the UI thread, and the fact that we are calling Application.DoEvents() inside the loop means that the async operation is performed "asynchronously" inside the UI thread. And because that loop takes place inside the try clause then any exception thrown by the async method will be caught by the corresponding catch clause. (At least this is the behavior I have observed.)
That said,
Is it ever a good idea to call Application.DoEvents() inside a loop in order to keep the UI responsive while waiting for an async operation to complete before moving on to doing more work inside the same UI thread?
Even if it's not a good idea to call Application.DoEvents() inside a loop, why does the application quietly exit (no window freezes, no error messages) after the catch clause handles the exception thrown by PerformAsyncOperation()?