ManualResetEvent浅谈

ManualResetEvent浅谈C#中ManualResetEvent的开关作用贴代码usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespacetest01{clas…

大家好,又见面了,我是你们的朋友全栈君。

C#中ManualResetEvent的开关作用

  1. 贴代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace test01
{ 
   
    class Program
    { 
   
        public static ManualResetEvent mre = new ManualResetEvent(true);
        public static void trmain()
        { 
   
            mre.Reset();
            mre.WaitOne();
           for(int i=0;i<10;i++)
            { 
   
                Thread.Sleep(1000);
                Console.Write("子线程编号:"+i.ToString()+"\r\n");
            }
        }
        static void Main(string[] args)
        { 
   
            Thread th1 = new Thread(trmain);
            th1.Start();
            for (int y = 0; y < 10; y++)
            { 
   
                Thread.Sleep(900);
                Console.Write("主线程编号:" + y.ToString()+"\r\n");
                if (y == 5)
                mre.Set();
                if (y == 7)
                mre.Reset();
            }
        }
    }
}

C#中执行结果

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161168.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号