• c#多线程(三)


    代码
           #region 信号使用于任务分解
            
            
    static EventWaitHandle ready=new AutoResetEvent(false);
            
    static EventWaitHandle go=new AutoResetEvent(false);
            
    static volatile string task="";
            
    static void WaitEventHandlerThreadTest()
            {
                
    //一个任务分解为多个线程来完成
                new Thread(DoWorker).Start();
                
    for (int i=0;i<5 ;i++ ) 
                {
                    ready.WaitOne();
    //首先工作线程等待,直到工作线程准备好。
                    task="a".PadRight(i,'h'); //给工作任务赋值
                    go.Set();  //然后开始执行
                }
                ready.WaitOne();task
    =null;go.Set();
            }
            
            
    static void DoWorker()
            {
                
    while(true)
                {
                    ready.Set(); 
    //说明该线程已经准备好。
                    go.WaitOne(); //然后等待退出
                    if(task==null)return//退出线程
                    Console.WriteLine(task);
                }
            }
            
    #endregion
            
            
    #region 测试线程jion方法
            
            
    static void JoinThreadTest()
            {
                Thread t
    =new Thread(delegate()
                           {
                                        
    //while(true)
                                        
    //{
                                            Console.ReadLine();
                                        
    //}
                           });
                t.Start();
                t.Join();
                Console.WriteLine(
    "Thread ReadLine is complete!");
            }
            
            
    #endregion
  • 相关阅读:
    通配符函数 MatchesMask 的使用
    WinAPI: GetComputerName 获取计算机名称
    TStringList 常用操作
    分割字符串 ExtractStrings
    磁盘类型 GetDriveType
    Delphi 的信息框相关函数
    Delphi 的运算符列表
    类型转换函数
    文件路径相关的字符串操作
    澳洲技术移民介绍
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1630924.html
Copyright © 2020-2023  润新知