C # code for thread suspension and enabling

Source: Internet
Author: User

 

 
 
  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.  &n

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.