|

Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. 超链接登录可见。 Thread.Resume has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. 超链接登录可见。 线程想暂停或则继续,以前用过Suspend和Resume两个方法,用这两个方法去操作线程,其实是有问题的,当时也没管,今天就拿出来研究下吧!
用上面两个方法去挂起和继续线程会有什么问题呢?
如果,这两个方法操作太快会抛异常的,例如:我们调用Suspend方法挂起线程,然后,立马调用Resume方法继续挂起的线程,这时就有可能会出问题。
因为,你在调用Suspend方法挂起线程后,线程并不一定立马会被挂起,这里需要处理时间,这时,你立马调用Resume方法继续挂起的线程,程序就立马挂掉了!
微软给的解释为:
请不要使用 Suspend 和 Resume 方法来同步线程活动。 有没有办法知道当你暂停执行线程什么代码。 如果在安全权限评估期间持有锁,您挂起线程中的其他线程 AppDomain 可能被阻止。 如果执行类构造函数时,您挂起线程中的其他线程 AppDomain 中尝试使用类被阻止。 可以很容易发生死锁。
//
// 摘要:
// 继续已挂起的线程。
//
// 异常:
// T:System.Threading.ThreadStateException:
// 该线程尚未启动、 已死或未处于挂起状态。
//
// T:System.Security.SecurityException:
// 调用方没有适当的 System.Security.Permissions.SecurityPermission。
[Obsolete("Thread.Resume has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. 超链接登录可见。", false)]
[SecuritySafeCritical]
public void Resume();
怎么解决这个问题呢?
ManualResetEvent 类
通知一个或多个正在等待的线程已发生事件。 此类不能被继承。
测试代码如下:
|
上一篇:C#发送短信到手机下一篇:sql去除数据中的空格
|