Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Is there a way I can go back to thread A while in Thread B's completion handler?</p> </blockquote> <p>No, you don't have the plumbing in place to marshal a call from one thread to another. That kind of plumbing is provided by the main thread of a GUI app. Out of necessity, user interfaces are pretty fundamentally thread-unsafe. There are several implementation details of a UI thread that support such marshaling. It acts like a consumer in a typical implementation of a <a href="http://en.wikipedia.org/wiki/Producer-consumer_problem">producer/consumer</a> threading model.</p> <p>Which requires a thread-safe queue and a loop in the consumer that reads the queue. You may recognize this as the message queue in a Windows GUI app. With the message loop that calls GetMessage to read notifications and act on them. Marshaling a call is now simple, you just post a message to the message queue, the UI thread reads it and executes the request. Posting is implemented by Control.BeginInvoke for Winforms and Dispatcher.BeginInvoke for WPF.</p> <p>You certainly can implement this synchronization mechanism yourself. The .NET 4 BlockingCollection class makes it easy. But <em>do</em> keep in mind that you have to fundamentally alter the way thread A executes. Staying responsive to requests posted to the queue is important. The kind of problem that a class like BackgroundWorker tries to solve. Keep in mind that the GUI message loop exists because it is <em>necessary</em>, UI isn't thread-safe. A console app doesn't (typically) have the same kind of burden, the console is thread-safe.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload