Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not exactly easy, but it's not really that hard either. What you need to do is a spin up a worker Thread that's setup as STA and you start up the Dispatcher runtime on it. Once you have that worker sitting there you can dispatch work to it from the unit test threads which are, obviously, not initialized for this kind of work. So, first, here's how you startup the dispatcher thread in your test setup:</p> <pre><code>this.dispatcherThread = new Thread(() =&gt; { // This is here just to force the dispatcher infrastructure to be setup on this thread Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =&gt; { Trace.WriteLine("Dispatcher worker thread started."); })); // Run the dispatcher so it starts processing the message loop Dispatcher.Run(); }); this.dispatcherThread.SetApartmentState(ApartmentState.STA); this.dispatcherThread.IsBackground = true; this.dispatcherThread.Start(); </code></pre> <p>Now, if you want to cleanly shut that thread down in your test cleanup, which I recommend you do, you simply do the following:</p> <pre><code>Dispatcher.FromThread(this.dispatcherThread).InvokeShutdown(); </code></pre> <p>So, all that infrastructure stuff out of the way, here's all you need to do in your test to execute on that thread.</p> <pre><code>public void MyTestMethod { // Kick the test off on the dispatcher worker thread synchronously which will block until the work is competed Dispatcher.FromThread(this.dispatcherThread).Invoke(new Action(() =&gt; { // FromCurrentSynchronizationContext will now resolve to the dispatcher thread here })); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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