程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#線程暫停與開啟的代碼

C#線程暫停與開啟的代碼

編輯:C#基礎知識

 

  1. using System;  
  2. using System.ComponentModel;  
  3. using System.Windows.Forms;  
  4. using System.Threading;  
  5.  
  6. namespace AutoResetEventTest  
  7. {  
  8.     public partial class Form1 : Form  
  9.     {  
  10.         private ManualResetEvent manualResetEvent;  
  11.         private AutoResetEvent auto;  
  12.         private bool suspend;  
  13.         private AsyncOperation asyncOperation;  
  14.  
  15.         public delegate void InvokeDelegate(string str);  
  16.  
  17.         private InvokeDelegate invokeDelegate;  
  18.         const string str = "Test";  
  19.         private bool formClosed;  
  20.  
  21.         public Form1()  
  22.         {  
  23.             InitializeComponent();  
  24.             manualResetEvent = new ManualResetEvent(false);  
  25.             auto = new AutoResetEvent(true);  
  26.             asyncOperation = AsyncOperationManager.CreateOperation(null);  
  27.             invokeDelegate = new InvokeDelegate(this.SafeInvoke);  
  28.             this.FormClosed += delegate 
  29.                                    {  
  30.                                        this.formClosed = true;  
  31.                                        this.auto.Close();  
  32.                                    };  
  33.         }  
  34.  
  35.         private void btnStart_Click(object sender, EventArgs e)  
  36.         {  
  37.             this.btnStart.Enabled = false;  
  38.             this.btnSuspend.Enabled = true;  
  39.             ThreadPool.QueueUserWorkItem(delegate 
  40.                                              {  
  41.                                                  SafeInvoke();  
  42.  
  43.                                                  //this.BeginInvoke(invokeDelegate, new object[] { str });  
  44.                                              });  
  45.         }  
  46.  
  47.         private void btnSuspend_Click(object sender, EventArgs e)  
  48.         {  
  49.             this.btnSuspend.Enabled = false;  
  50.             this.btnResume.Enabled = true;  
  51.             this.suspend = true;  
  52.             manualResetEvent.Reset();  
  53.         }  
  54.  
  55.         private void SafeInvoke(string s)  
  56.         {  
  57.             while (true)  
  58.             {  
  59.                 if (formClosed)  
  60.                     return;  
  61.  
  62.                 Thread.Sleep(200);  
  63.  
  64.                 if (suspend)  
  65.                     this.auto.WaitOne();  
  66.                 this.txtMessageBox.AppendText(s);  
  67.             }  
  68.         }  
  69.  
  70.         private void SafeInvoke()  
  71.         {  
  72.             while (true)  
  73.             {  
  74.                 if (formClosed)  
  75.                     return;  
  76.  
  77.                 Thread.Sleep(200);  
  78.  
  79.                 if (suspend)  
  80.                     //this.auto.WaitOne();  
  81.                     manualResetEvent.WaitOne();  
  82.  
  83.                 asyncOperation.Post(delegate 
  84.                                     {  
  85.                                         this.txtMessageBox.AppendText(str);  
  86.                                     }, str);  
  87.             }  
  88.  
  89.         }  
  90.  
  91.         private void btnResume_Click(object sender, EventArgs e)  
  92.         {  
  93.             this.btnResume.Enabled = false;  
  94.             this.btnSuspend.Enabled = true;  
  95.             this.suspend = false;  
  96.             //this.auto.Set();  
  97.             manualResetEvent.Set();  
  98.         }  
  99.     }  

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved