JD2022-TU1/main/tools/framework/JD.Threading/WakeUpThread.cs

75 lines
No EOL
1.7 KiB
C#

using System;
using JD.Kernel.StaticHelpers;
namespace JD.Threading
{
/// <summary>
/// A concrete thread that starts on demand.
/// </summary>
public class WakeUpThread : WakeUpThreadBase
{
#region Delegates
public delegate void QueueUserWorkItemDelegate(Action action);
#endregion
#region Variables
private readonly Action m_threadAction;
private readonly QueueUserWorkItemDelegate m_threadDispatchStrategy;
#endregion
#region Constructor
public WakeUpThread(Action threadAction)
: this(threadAction, null)
{
}
/// <summary>
/// Allows to specify the action used to invoke the action on a thread pool.
/// </summary>
public WakeUpThread(Action threadAction, QueueUserWorkItemDelegate threadDispatchStrategy)
{
Throw.IfNull(threadAction, "threadAction");
m_threadAction = threadAction;
m_threadDispatchStrategy = threadDispatchStrategy;
}
#endregion
#region Methods
protected override void DoWork()
{
m_threadAction();
}
public new void Join()
{
base.Join();
}
public new void WakeUp()
{
base.WakeUp();
}
protected override void QueueUserWorkItem(System.Threading.WaitCallback waitCallBack)
{
if (m_threadDispatchStrategy != null)
{
m_threadDispatchStrategy(() => waitCallBack(null));
}
else
{
base.QueueUserWorkItem(waitCallBack);
}
}
#endregion
}
}