Export (0) Print
Expand All

Dispatcher Class

Provides services for managing the queue of work items for a thread.

System.Object
  System.Windows.Threading.Dispatcher

Namespace:  System.Windows.Threading
Assembly:  System.Windows (in System.Windows.dll)

'Declaration
<CLSCompliantAttribute(True)> _
Public NotInheritable Class Dispatcher

The Dispatcher type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneBeginInvoke(Action)Executes the specified delegate asynchronously on the thread the Dispatcher is associated with.
Public methodSupported by Silverlight for Windows PhoneBeginInvoke(Delegate, Object())Executes the specified delegate asynchronously with the specified array of arguments on the thread the Dispatcher is associated with.
Public methodSupported by Silverlight for Windows PhoneCheckAccessDetermines whether the calling thread is the thread associated with this Dispatcher.
Public methodSupported by Silverlight for Windows PhoneEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The Dispatcher class currently provides support only for running code on the user interface (UI) thread from a non-UI thread.

You can access the Dispatcher object for the UI thread through the DependencyObject.Dispatcher and ScriptObject.Dispatcher properties. These are instance methods, but instances of these types are frequently inaccessible from non-UI threads. However, the application's Deployment object is a DependencyObject, and it is available on any thread through its Current property.

You can call the CheckAccess method to determine whether the caller is on the UI thread. If the caller is not on the UI thread, you can call BeginInvoke to run the specified delegate on the UI thread.

The following code example demonstrates how to use this class.


Private Delegate Sub AddTextDelegate(ByVal p As Panel, ByVal text As String)

Private Sub AddText(ByVal p As Panel, ByVal text As String)
    p.Children.Clear()
    Dim t As New TextBlock
    t.Text = text
    p.Children.Add(t)
End Sub

Private Sub TestBeginInvokeWithParameters(ByVal p As Panel)
    If p.Dispatcher.CheckAccess() _
        Then AddText(p, "Added directly.") _
        Else p.Dispatcher.BeginInvoke(New AddTextDelegate( _
            AddressOf AddText), p, "Added by Dispatcher.")
End Sub


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft