80 lines
No EOL
1.8 KiB
C#
80 lines
No EOL
1.8 KiB
C#
using System.Diagnostics;
|
|
using JD.Kernel.StaticHelpers;
|
|
|
|
namespace JD.Threading.Dispatch
|
|
{
|
|
|
|
public static class GuiDispatcher
|
|
{
|
|
#region Variables
|
|
|
|
private static Dispatcher ms_dispatcher;
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
static GuiDispatcher()
|
|
{
|
|
SetDispatchStrategy(new FreeThreadDispatchStrategy());
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public static Dispatcher Current
|
|
{
|
|
get { return ms_dispatcher; }
|
|
}
|
|
|
|
public static bool IsOnUIThread
|
|
{
|
|
get { return !Current.Strategy.InvokeRequired; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Throws if the method is not called on the UI thread.
|
|
/// </summary>
|
|
[Conditional("DEBUG")]
|
|
public static void AssertIsUIThread()
|
|
{
|
|
Throw.Assert(IsOnUIThread, "Method was not called on UI thread");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Internal
|
|
/// </summary>
|
|
/// <param name="dispatchStrategy"></param>
|
|
public static void SetDispatchStrategy(IThreadDispatchStrategy dispatchStrategy)
|
|
{
|
|
ms_dispatcher = new ConcreteDispatcher(dispatchStrategy);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Inner Types
|
|
|
|
private class ConcreteDispatcher : Dispatcher
|
|
{
|
|
private readonly IThreadDispatchStrategy m_strategy;
|
|
|
|
public ConcreteDispatcher(IThreadDispatchStrategy strategy)
|
|
{
|
|
Throw.IfNull(strategy, "strategy");
|
|
m_strategy = strategy;
|
|
}
|
|
|
|
public override IThreadDispatchStrategy Strategy
|
|
{
|
|
get { return m_strategy; }
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |