This article explains how you can work with the dispatcher, like making use of the
asychronous calls can be made to the thread that the Dispatcher object owns, initiating
shutdown sychronously and asynchronously. and much other stuff on the dispatcher
object.
The Actual wrapper class(ThreadDispatcherUtility) is below the sample code. I have bulletted the uses.
1. Before calling the base class implementation of the Startup method of your application,
you can Subscribe to application Idle event
//Subscribe to Application Idle event.
SubscribeApplicationIdle();
base.OnStartup(e);
/// <summary>/
// Subscribes application idle.
/// </summary>
private void SubscribeApplicationIdle()
{
ThreadDispatcherUtility.Instance.BeginInvoke(DispatcherPriority.ApplicationIdle, OnAppIdle);
ThreadDispatcherUtility.Instance.DoEvents();
}
///
<summary>
/// Called when [app idle].
/// </summary>
private void OnAppIdle()
{
//Do all the stuff that you need to do here.
}
2. When you want to process all the messages in the Queue. you can use the DoEvents
method of the Wrapper class.
/// <summary>
/// Does the events.
/// </summary>
public void DoEvents()
{
ThreadDispatcherUtility.Instance.DoEvents();
}
3. When you need to update an object on another thread, like a Splash Screen, you can
use the following method.
/// <summary>
/// Updates the splash message core.
/// </summary>
/// <param name="message">The message.</param>
private void UpdateSplashMessageInTheSplash(object splashMessage)
{
//Actual implementation in the splash object
splash.UpdateProgressMessage((string)splashMessage);
}
Call the above method using the Wrapper class.
/// <summary>
/// Updates the message in splash screen
/// </summary>
/// <param name="message"></param>
public void UpdateSplashMessage(string message)
{
if (splash != null)
{
ThreadDispatcherUtility.Instance.Invoke(DispatcherPriority.Background, UpdateSplashMessageInTheSplash, message);
ThreadDispatcherUtility.Instance.DoEvents();
}
}
And many more....
The actual Wrapper.
#region
Namespaces
using
System;
using System.Windows.Threading;
#endregion Namespaces
namespace
Utilities
{
/// <summary>
/// DoEvents delegate.
/// </summary>
public delegate void DoEvents(object arg);
/// <summary>
/// Thread Dispatcher class.
/// </summary>
public class ThreadDispatcherUtility
{
#region Fields
/// <summary>
/// ThreadDispatcherUtility object.
/// </summary>
private static ThreadDispatcherUtility dispatcher;
/// <summary>
/// Dispatcher readonly object.
/// </summary>
private readonly Dispatcher uiDispatcher;
#endregion
#region
Private Constructor
/// <summary>
/// Initializes a new instance of the <see cref="ThreadDispatcherUtility"/> class.
/// </summary>
private ThreadDispatcherUtility()
{
uiDispatcher = Dispatcher.CurrentDispatcher;
}
#endregion
#region
Singleton
/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static ThreadDispatcherUtility Instance
{
get
{
if(dispatcher == null)
InitializeDispatcher();
return dispatcher;
}
}
/// <summary>
/// Initializes the dispatcher.
/// </summary>
private static void InitializeDispatcher()
{
dispatcher = new ThreadDispatcherUtility();
}
#endregion
#region
Invoke Methods
/// <summary>
/// Begins the invoke.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="method">The method.</param>
public virtual void BeginInvoke(DispatcherPriority priority, Delegate method)
{
if (!uiDispatcher.HasShutdownStarted)
uiDispatcher.BeginInvoke(priority, method);
}
/// <summary>
/// Begins the invoke.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="method">The method.</param>
/// <param name="arg">The arg.</param>
public virtual void BeginInvoke(DispatcherPriority priority, Delegate method, object arg)
{
if (!uiDispatcher.HasShutdownStarted)
uiDispatcher.BeginInvoke(priority, method, arg);
}
/// <summary>
/// Begins the invoke.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="method">The method.</param>
/// <param name="arg">The arg.</param>
/// <param name="param">The param.</param>
public virtual void BeginInvoke(DispatcherPriority priority, Delegate method, object arg, params object[] param)
{
if (!uiDispatcher.HasShutdownStarted)
uiDispatcher.BeginInvoke(priority, method, arg, param);
}
/// <summary>
/// Begins the invoke.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="action">The action.</param>
/// <param name="arg">The arg.</param>
public void BeginInvoke(DispatcherPriority priority, Func<object, object> action, object arg)
{
if (!uiDispatcher.HasShutdownStarted)
{
uiDispatcher.BeginInvoke(priority, action, arg)
}
}
/// <summary>
/// Begins the invoke.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="action">The action.</param>
public void BeginInvoke(DispatcherPriority priority, Action action)
{
if (!uiDispatcher.HasShutdownStarted)
{
uiDispatcher.BeginInvoke(priority, action);
}
}
/// <summary>
/// Invokes the specified priority.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="method">The method.</param>
/// <param name="arg">The arg.</param>
/// <param name="param">The param.</param>
public virtual void Invoke(DispatcherPriority priority, Delegate method, object arg, params object[] param)
{
if (!uiDispatcher.HasShutdownStarted)
uiDispatcher.Invoke(priority, method, arg, param);
}
/// <summary>
/// Invokes the specified priority.
/// </summary>/// <param name="priority">The priority.</param>
/// <param name="method">The method.</param>
/// <param name="arg">The arg.</param>
public virtual void Invoke(DispatcherPriority priority, Delegate method, object arg)
{
if (!uiDispatcher.HasShutdownStarted)
{
if (arg == null)
{
uiDispatcher.Invoke(priority, method);
}
else
{
uiDispatcher.Invoke(priority, method, arg);
}
}
}
/// <summary>
/// Invokes the specified priority.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="method">The method.</param>
public virtual void Invoke(DispatcherPriority priority, Delegate method)
{
Invoke(priority, method, null);
}
/// <summary>
/// Invokes the specified priority.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="action">The action.</param>
/// <param name="arg">The arg.</param>
public virtual void Invoke(DispatcherPriority priority, Action<object> action, object arg)
{
if(!uiDispatcher.HasShutdownStarted)
{
if(arg == null)
{
uiDispatcher.Invoke(priority, action);
}
else
{
uiDispatcher.Invoke(priority, action, arg);
}
}
}
/// <summary>
/// Invokes the specified priority.
/// </summary>
/// <param name="priority">The priority.</param>
/// <param name="action">The action.</param>
public virtual void Invoke(DispatcherPriority priority, Action<object> action)
{
Invoke(priority, action, null);
}
#endregion
#region
DoEvents
/// <summary>
/// Does the events.
/// </summary>
public void DoEvents()
{
var frame = new DispatcherFrame();
BeginInvoke(DispatcherPriority.Loaded, new DoEvents(arg =>
{
var f = arg as DispatcherFrame;
if (f != null)
f.Continue = false;
}
), frame);
Dispatcher.PushFrame(frame);
}
#endregion
}
}