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、

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM