C#中,让线程挂起。不能使用Thead.Suspend(), 这个函数在.NET中已经被否决了,具体原因没有深入研究。微软推荐使用System.Threading中的函数,比如Monitor, Mutex, Event等。
在QQ好友空间访客提取的项目中我使用了ManualResetEvent这个Event类,具体用法如下:
类字段定义:
private ManualResetEvent _event = new ManualResetEvent (true ); //线程事件
private bool _pause = false; //线程是否暂停
工作线程函数中:
if (_pause)
{
SetStatus( "已暂停");
}
_event.WaitOne();
暂停函数中:
SetStatus("暂停中...");
_event.Reset();
_pause = true;
恢复函数中:
if (_pause)
{
//to resume thread
_event.Set();
_pause = false;
return;
}
本文为“技术点滴”的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。