Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ideally, store a single instance of <code>Dispatcher</code> that you can use elsewhere without having the thread check on it.</p> <p>Calling any singleton .Current instance may in fact cause a cross-thread access check to be invoked. By storing it first, you can avoid this to actually get the shared instance.</p> <p>I use a "SmartDispatcher" that uses a dispatcher when called off-thread, and just invokes otherwise. It solves this sort of issue.</p> <p>Post: <a href="http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/" rel="noreferrer">http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/</a> </p> <p>Code:</p> <pre><code>// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. using System.ComponentModel; namespace System.Windows.Threading { /// &lt;summary&gt; /// A smart dispatcher system for routing actions to the user interface /// thread. /// &lt;/summary&gt; public static class SmartDispatcher { /// &lt;summary&gt; /// A single Dispatcher instance to marshall actions to the user /// interface thread. /// &lt;/summary&gt; private static Dispatcher _instance; /// &lt;summary&gt; /// Backing field for a value indicating whether this is a design-time /// environment. /// &lt;/summary&gt; private static bool? _designer; /// &lt;summary&gt; /// Requires an instance and attempts to find a Dispatcher if one has /// not yet been set. /// &lt;/summary&gt; private static void RequireInstance() { if (_designer == null) { _designer = DesignerProperties.IsInDesignTool; } // Design-time is more of a no-op, won't be able to resolve the // dispatcher if it isn't already set in these situations. if (_designer == true) { return; } // Attempt to use the RootVisual of the plugin to retrieve a // dispatcher instance. This call will only succeed if the current // thread is the UI thread. try { _instance = Application.Current.RootVisual.Dispatcher; } catch (Exception e) { throw new InvalidOperationException("The first time SmartDispatcher is used must be from a user interface thread. Consider having the application call Initialize, with or without an instance.", e); } if (_instance == null) { throw new InvalidOperationException("Unable to find a suitable Dispatcher instance."); } } /// &lt;summary&gt; /// Initializes the SmartDispatcher system, attempting to use the /// RootVisual of the plugin to retrieve a Dispatcher instance. /// &lt;/summary&gt; public static void Initialize() { if (_instance == null) { RequireInstance(); } } /// &lt;summary&gt; /// Initializes the SmartDispatcher system with the dispatcher /// instance. /// &lt;/summary&gt; /// &lt;param name="dispatcher"&gt;The dispatcher instance.&lt;/param&gt; public static void Initialize(Dispatcher dispatcher) { if (dispatcher == null) { throw new ArgumentNullException("dispatcher"); } _instance = dispatcher; if (_designer == null) { _designer = DesignerProperties.IsInDesignTool; } } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public static bool CheckAccess() { if (_instance == null) { RequireInstance(); } return _instance.CheckAccess(); } /// &lt;summary&gt; /// Executes the specified delegate asynchronously on the user interface /// thread. If the current thread is the user interface thread, the /// dispatcher if not used and the operation happens immediately. /// &lt;/summary&gt; /// &lt;param name="a"&gt;A delegate to a method that takes no arguments and /// does not return a value, which is either pushed onto the Dispatcher /// event queue or immediately run, depending on the current thread.&lt;/param&gt; public static void BeginInvoke(Action a) { if (_instance == null) { RequireInstance(); } // If the current thread is the user interface thread, skip the // dispatcher and directly invoke the Action. if (_instance.CheckAccess() || _designer == true) { a(); } else { _instance.BeginInvoke(a); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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