英雄杀手游真萧太后:C#线程暂停与开启的代码
来源:百度文库 编辑:九乡新闻网 时间:2024/09/21 09:30:08
- 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();
- }
- }
- }
C#线程暂停与开启的代码
线程的暂停与重新启动
C#教程:线程的优先权
C#线程挂起的问题
C#多线程编程:线程的启动
c#线程相关概念
C#教程:线程同步
C#教程:线程应用实例
线程的挂起与继续
C#入门的一些代码二
如何终止和暂停线程和进程?
进程与线程的区别2
进程与线程的区别1
进程与线程的区别123
进程与线程 - orbit的专栏
C#代码之plane
线程--管理线程(使线程中止,暂停,挂起等)(示例下载)
C#接口-接口的继承 - C#编程 - Asp.net C# 学习与研究
C#实现汉字转换为拼音缩写的代码
C#连接六类数据库的代码集
200行C代码实现简单线程池 - Linux内核编程 - 举世无双的学习之路
c++与c#区别
网站安全性:C#防SQL注入代码的实现方法 - 51CTO.COM
C# byte及char FileStream 与StreamReader的区别