c# – 暂停线程,直到手动恢复
发布时间:2020-12-15 06:42:24 所属栏目:百科 来源:网络整理
导读:参见英文答案 How to pause/suspend a thread then continue it?3 在c#中手动恢复线程之前,如何将线程置于暂停/休眠状态? 目前我正在中止线程,但这不是我正在寻找的.线程应该睡眠/暂停,直到它触发它唤醒为止. 解决方法 你应该通过 ManualResetEvent这样做.
参见英文答案 >
How to pause/suspend a thread then continue it?3
在c#中手动恢复线程之前,如何将线程置于暂停/休眠状态? 目前我正在中止线程,但这不是我正在寻找的.线程应该睡眠/暂停,直到它触发它唤醒为止. 解决方法
你应该通过
ManualResetEvent这样做.
ManualResetEvent mre = new ManualResetEvent(); mre.WaitOne(); // This will wait 在另一个线程上,显然您需要对ManualResetEvent实例的引用. mre.Set(); // Tells the other thread to go again 一个完整的例子,将打印一些文本,等待另一个线程做某事,然后恢复: class Program { private static ManualResetEvent mre = new ManualResetEvent(false); static void Main(string[] args) { Thread t = new Thread(new ThreadStart(SleepAndSet)); t.Start(); Console.WriteLine("Waiting"); mre.WaitOne(); Console.WriteLine("Resuming"); } public static void SleepAndSet() { Thread.Sleep(2000); mre.Set(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |