日期:2014-05-18 浏览次数:21017 次
class TestClient { private Thread workThread; private SynchronizationContext mainThreadSynContext; public event EventHandler Connected; public TestClient()//构造函数当然是主线程执行的 { mainThreadSynContext = SynchronizationContext.Current; workThread = new Thread(new ThreadStart(DoWork)); } private void OnConnected(object state)//由于是主线程的同步对象Post调用,这个是在主线程中执行的 { if (Connected != null) Connected(this, EventArgs.Empty); } private void DoWork()//这个是workThread线程执行的 { //这儿做些事(连接什么的。。。) //这而干完了 if (Connected != null) mainThreadSynContext.Post(new SendOrPostCallback(OnConnected), null); } }