C#等待事件触发(类似WaitForSingleObject())_线程

1、c# 等待事件触发 类似c++的WaitForSingleObject()_百度知道.html(https://zhidao.baidu.com/question/1539843583632581347.html

ManualResetEvent mre = new ManualResetEvent(false);
 
// 你的线程
private void MyThread( )
{
   // 等待10毫秒,检查 mre是否设置为true。如果没有,继续循环
   while(!mre.WaitOne(10))
   {
        //……
   }
} 
 
//在主线程中
static void Main()
{
    //……
     
    //设置 mrs为true,通知线程终止
    mrs.Set( );
     
    //……
}

 

2、ZC:搜索:"ManualResetEvent"、"AutoResetEvent"

3、

4、搜到的线程使用方式:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading;  
 
namespace MyThread  
{  
    class Program  
    {
        // ZC: 静态函数
        public static void myStaticThreadMethod()  
        {  
            Console.WriteLine("myStaticThreadMethod");  
        }

        // ZC: 类函数
        public void myThreadMethod()  
        {  
            Console.WriteLine("myThreadMethod");  
        } 

        static void Main(string[] args)  
        {
          // ZC: 两种 方式
            Thread thread1 = new Thread(myStaticThreadMethod); 
            Thread thread2 = new Thread(new Program().myThreadMethod);

            // ZC: 指明是否是后台线程(主线程结束并不影响其它前台线程的执行,只有所有前台线程都结束,程序才结束)
            thread2.IsBackground = true;
            thread1.Start();  // 只有使用Start方法,线程才会运行  
        }  
    }  
}  

 

5、

6、c#子线程与主线程之间的通信 - qq_23127851的博客 - CSDN博客.html(https://blog.csdn.net/qq_23127851/article/details/78270988

  ZC:SynchronizationContext 方式,子线程 调用 主线程 函数

7、

8、

9、

10、

 

posted @ 2019-07-12 16:06  csskill  阅读(5543)  评论(0编辑  收藏  举报