时间:2016-02-16 00:00 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【.NET中STAThread的使用详解】,下面是详细的讲解!
.NET中STAThread的使用详解
namespace CLK.Threading
{
public class STAThread
{
// Enum
private enum ThreadState
{
Started,
Stopping,
Stopped,
}
// Fields
private readonly object _syncRoot=new object();
private readonly BlockingQueue<Action> _actionQueue=null;
private Thread _thread=null;
private ManualResetEvent _threadEvent=null;
private ThreadState _threadState=ThreadState.Stopped;
// Constructor
public STAThread()
{
// ActionQueue
_actionQueue=new BlockingQueue<Action>();
// ThreadEvent
_threadEvent=new ManualResetEvent(true);
// ThreadState
_threadState=ThreadState.Stopped;
}
// Methods
public void Start()
{
// Sync
lock (_syncRoot)
{
// ThreadState
if (_threadState !=ThreadState.Stopped) throw new InvalidOperationException();
_threadState=ThreadState.Started;
}
// Thread
_thread=new Thread(this.Operate);
_thread.Name=string.Format("Class:{0}, Id:{1}", "STAThread", _thread.ManagedThreadId);
_thread.IsBackground=false;
_thread.Start();
}
public void Stop()
{
// Sync
lock (_syncRoot)
{
// ThreadState
if (_threadState !=ThreadState.Started) throw new InvalidOperationException();
_threadState=ThreadState.Stopping;
// ActionQueue
_actionQueue.Release();
}
// Wait
_threadEvent.WaitOne();
}
public void Post(SendOrPostCallback callback, object state)
{
#region Contracts
if (callback==null) throw new ArgumentNullException();
#endregion
// Action
Action action=delegate()
{
try
{
callback(state);
}
catch (Exception ex)
{
Debug.Fail(string.Format("Delegate:{0}, State:{1}, Message:{2}", callback.GetType(), "Exception", ex.Message));
}
};
// Sync
lock (_syncRoot)
{
// ThreadState
if (_threadState !=ThreadState.Started) throw new InvalidOperationException();
// ActionQueue
_actionQueue.Enqueue(action);
}
}
public void Send(SendOrPostCallback callback, object state)
{
#region Contracts
if (callback==null) throw new ArgumentNullException();
#endregion
// Action
ManualResetEvent actionEvent=new ManualResetEvent(false);
Action action=delegate()
{
try
{
callback(state);
}
catch (Exception ex)
{
Debug.Fail(string.Format("Delegate:{0}, State:{1}, Message:{2}", callback.GetType(), "Exception", ex.Message));
}
finally
{
actionEvent.Set();
}
};
// Sync
lock (_syncRoot)
{
// ThreadState
if (_threadState !=ThreadState.Started) throw new InvalidOperationException();
// ActionQueue
if (Thread.CurrentThread !=_thread)
{
_actionQueue.Enqueue(action);
}
}
// Execute
if (Thread.CurrentThread==_thread)
{
action();
}
// Wait
actionEvent.WaitOne();
}
private void Operate()
{
try
{
// Begin
_threadEvent.Reset();
// Operate
while (true)
{
// Action
Action action=_actionQueue.Dequeue();
// Execute
if (action !=null)
{
action();
}
// ThreadState
if (action==null)
{
lock (_syncRoot)
{
if (_threadState==ThreadState.Stopping)
{
return;
}
}
}
}
}
finally
{
// End
lock (_syncRoot)
{
_threadState=ThreadState.Stopped;
}
_threadEvent.Set();
}
}
}
}
关于.NET中STAThread的使用详解的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 【asp】asp.net开发中常见公共捕获异常方式总结(
- 【Visual】分享Visual Studio原生开发的10个调试
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
