英雄杀手游真萧太后:C#线程暂停与开启的代码

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 03:45:29
  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.     }