读书人

C#中过程的暂停和继续

发布时间: 2013-05-02 09:39:29 作者: rapoo

C#中进程的暂停和继续
我想在一个程序里添加一个按钮,第一次单机时暂停,再次单击继续。怎样实现啊,用的 game.Resume();和game.Suspend();说过时了,担心的没找到,不知道怎么实现
[解决办法]
http://topic.csdn.net/u/20120214/17/268f33f2-9a46-460e-a00a-b35a6eee2750.html
[解决办法]
Suspend Resume 或者加线程锁
如果你程序是单线程的

提供一个思路
假如你的那个线程需要无线的跑 你循环就好了 加入一个bool类型的值
每次线程跑一遍前提是要判断这个值是否为true
按钮单机事件处理bool值变化


或者也可以这样Application.DoEvents();
死循环中跳出http://blog.csdn.net/way5040/article/details/7770675
[解决办法]
是进程还是线程暂停?
进程暂停,是不是会卡住?
如果是线程就好办了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private static bool threadSwitch = false;//定义一个全局变量来控制线程

private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "继续")
{//开始
button1.Text = "暂停";
threadSwitch = true;

ThreadPool.QueueUserWorkItem(h =>
{//在这里执行线程工作
while (threadSwitch == true)
{
textBox1.Invoke(new Action(delegate
{
textBox1.Text = DateTime.Now.ToString();
}));

}
});


}
else
{//暂停
button1.Text = "继续";
threadSwitch = false;
}
}
}
}

读书人网 >C#

热点推荐