C#线程暂停与开启的代码

2016-02-19 12:22 1 1 收藏

今天图老师小编给大家精心推荐个C#线程暂停与开启的代码教程,一起来看看过程究竟如何进行吧!喜欢还请点个赞哦~

【 tulaoshi.com - 编程语言 】

 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

using System;  using System.ComponentModel;  using System.Windows.Forms;  using System.Threading;   namespace AutoResetEventTest  {      public partial class Form1 : Form      {          private ManualResetEvent manualResetEvent;          private AutoResetEvent auto;          private bool suspend;          private AsyncOperation asyncOperation;           public delegate void InvokeDelegate(string str);           private InvokeDelegate invokeDelegate;          const string str = "Test";          private bool formClosed;           public Form1()          {              InitializeComponent();              manualResetEvent = new ManualResetEvent(false);              auto = new AutoResetEvent(true);              asyncOperation = AsyncOperationManager.CreateOperation(null);              invokeDelegate = new InvokeDelegate(this.SafeInvoke);              this.FormClosed += delegate                                    {                                         this.formClosed = true;                                         this.auto.Close();                                     };          }           private void btnStart_Click(object sender, EventArgs e)          {              this.btnStart.Enabled = false;              this.btnSuspend.Enabled = true;              ThreadPool.QueueUserWorkItem(delegate                                              {                                                   SafeInvoke();                                                    //this.BeginInvoke(invokeDelegate, new object[] { str });                                               });          }           private void btnSuspend_Click(object sender, EventArgs e)          {              this.btnSuspend.Enabled = false;              this.btnResume.Enabled = true;              this.suspend = true;              manualResetEvent.Reset();          }           private void SafeInvoke(string s)          {              while (true)              {                  if (formClosed)                      return;                   Thread.Sleep(200);                   if (suspend)                      this.auto.WaitOne();                  this.txtMessageBox.AppendText(s);              }          }           private void SafeInvoke()          {              while (true)              {                  if (formClosed)                      return;                   Thread.Sleep(200);                   if (suspend)                      //this.auto.WaitOne();                      manualResetEvent.WaitOne();                   asyncOperation.Post(delegate                                     {                                          this.txtMessageBox.AppendText(str);                                      }, str);              }           }           private void btnResume_Click(object sender, EventArgs e)          {              this.btnResume.Enabled = false;              this.btnSuspend.Enabled = true;              this.suspend = false;              //this.auto.Set();              manualResetEvent.Set();          }      }  } 

 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1600737.html

延伸阅读
using System;  using System.DirectoryServices;  using System.Collections;  using System.Text.RegularExpressions;  using System.Text;  /**   * @author 吴海燕   * @email  wuhy80-usual@yahoo.com   * 2004-...
如同java一样,在c#中写一个多线程应用是非常简单的,本章将介绍如何在c#种开发多线程程序。在.net中线程是由System.Threading 名字空间所定义的。所以你必须包含这个名字空间。   using System.Threading;    开始一个线程      System.Threading 名字空间的线程类描述了一个线程对象,通过使用类对象,你可以...
如何控制好多个线程相互之间的联系,不产生冲突和重复,这需要用到互斥对象,即:System.Threading 命名空间中的 Mutex 类。 我们可以把Mutex看作一个出租车,乘客看作线程。乘客首先等车,然后上车,最后下车。当一个乘客在车上时,其他乘客就只有等他下车以后才可以上车。而线程与Mutex对象的关系也正是如此,线程使用Mutex.WaitOne()方法等...
在编写多线程程序时无可避免会碰到线程的同步问题。什么是线程的同步呢? 举个例子:假如在一个公司里面有一个变量记录某人T的工资count=100,有两个主管A和B(即工作线程)在早一些时候拿了这个变量的值回去 ,过了一段时间A主管将T的工资加了5块,并存回count变量,而B主管将T的工资减去3块,并存回count变量。好了,本来T君可以得到102块...
whois.aspx  % @Page Language="C#" %  % @Import Namespace="System.Net.Sockets" %  % @Import Namespace="System.Text" %  % @Import Namespace="System.IO" %  % @Import Namespace="System.Collections" %  script ...

经验教程

407

收藏

19
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部