• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解C# 线程的挂起与唤醒

c# 搞代码 4年前 (2022-01-09) 56次浏览 已收录 0个评论
文章目录[隐藏]

     如果说C#和C++有什么不同,博主不得不说,对于异步的支持程度是C#的一一个伟大的进步。

    其实早期的C++都没有异步,并发的概念。博主第一次使用C++创建异步程序的时候,是使用boost库的内容进行实现的。相对而言,C#对于异步的支持可以说是相当的好。相信很多名词大家都很耳熟能详,比如说Thread,BeginInvoke,Delegate,backgroundworker等等。。。其实楼主在使用了这么多的异步操作过程中,还是觉得backgroudworker比较好用。

    当然,我们今天要说的和上面的无关。讲述的是如何在线程中进行挂起唤醒操作。

    假设,有一个Thread现在需要挂起,等到合适的时候再唤醒那么这个线程(消费者模式)。如果大家需要用Suspend,Resume操作,我建议还是要思考再三。以下是msdn原话(https://msdn.microsoft.com/zh-cn/library/system.threading.thread.suspend(v=vs.110).aspx):

  &本文来源gao($daima.com搞@代@#码$网nbsp; Do not use the Suspend and Resume methods to synchronize the activities of threads. You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked. Deadlocks can occur very easily.

     本篇文章要说的线程挂起与继续的方式其实是利用AutoResetEvent和ManualResetEvent的方法进行堵塞和继续的。

在介绍AutoResetEvent和ManualResetEvent之前,先介绍一个概念,就是线程中Set()和Reset()的区别。

  • set:指的是将一个事件设置为有信号,那么被这个事件堵塞的线程就会继续下去。
  • reset:指的是将一个事件设置为无信号,那么尝试继续的事件就会被堵塞。

一,AutoResetEvent类

     这个类的字面意思就能够解释一切:自动reset的事件,就是这个事件一旦set之后,如果某个线程堵塞被继续了,那么就会自动reset。下一次如果尝试继续,依然会被堵塞。

      其中AutoResetEvent类的构造函数有一个参数 是bool型。

     MSDN的解释是:

      Initializes a new instance of the AutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled.

    如果这个参数是true,那么第一次尝试继续就不会被阻塞。如果这个参数是false,那么第一次尝试继续就会被堵塞。

    以下是测试代码,取自MSDN:

using System;
using System.Threading;

// Visual Studio: Replace the default class in a Console project with 
//                the following class.
class Example
{
    private static AutoResetEvent event_1 = new AutoResetEvent(true);
    private static AutoResetEvent event_2 = new AutoResetEvent(false);

    static void Main()
    {
        Console.WriteLine("Press Enter to create three threads and start them.\r\n" +
                          "The threads wait on AutoResetEvent #1, which was created\r\n" +
                          "in the signaled state, so the first thread is released.\r\n" +
                          "This puts AutoResetEvent #1 into the unsignaled state.");
        Console.ReadLine();

        for (int i = 1; i < 4; i++)
        {
            Thread t = new Thread(ThreadProc);
            t.Name = "Thread_" + i;
            t.Start();
        }
        Thread.Sleep(250);

        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine("Press Enter to release another thread.");
            Console.ReadLine();
            event_1.Set();
            Thread.Sleep(250);
        }

        Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Press Enter to release a thread.");
            Console.ReadLine();
            event_2.Set();
            Thread.Sleep(250);
        }

        // Visual Studio: Uncomment the following line.
        //Console.Readline();
    }

    static void ThreadProc()
    {
        string name = Thread.CurrentThread.Name;

        Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
        event_1.WaitOne();
        Console.WriteLine("{0} is released from AutoResetEvent #1.", name);

        Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
        event_2.WaitOne();
        Console.WriteLine("{0} is released from AutoResetEvent #2.", name);

        Console.WriteLine("{0} ends.", name);
    }
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解C# 线程的挂起与唤醒

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址