比如,用户点击bootton1,启动线程,
private void bootton1_Click(object sender, EventArgs e)
{
Thread imputThread = new Thread(new ThreadStart(ImputData));
imputThread.Start();
}用户点击botton2时结束次线程。应该怎么做。谢谢。
private void bootton1_Click(object sender, EventArgs e)
{
Thread imputThread = new Thread(new ThreadStart(ImputData));
imputThread.Start();
}用户点击botton2时结束次线程。应该怎么做。谢谢。
解决方案 »
- c# 两个form同时运行
- 除了哈希表以外,还有其它比较好的存储方式没有?
- WinForm 中 关于DataGrid 拖拽的问题!
- treeview 节点值
- 请问各位解决一个关于数据时页面被访问导致出错的问题~!
- 在asp.net中,如何让客户自定义模板?
- 用commandbuilder和dataadapter更新数据库时,在dataadapter.update(ds)中的ds的内容是只包括要更新的内容还是包括当前数据库里的内容和
- 如何通过一个对象的字符串描述返回该对象的实例?
- 倾家荡产问个问题~~~图片存到SQL中,在PICTUREBOX中显示出来
- [SOS] 如何在TreeView的節點中嵌入CheckBox或者RadioButton? [SOS]
- 我设计一个计数器,遇到一个清空数据再计算的问题
- c#是否可以拦截CreateProcess()
{
//这里我要怎么写?谢谢各位高手了。
}
private void Button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread imputThread = new System.Threading.Thread(new System.Threading.ThreadStart(ImputData));
imputThread.Start();
}private void Button2_Click(object sender, System.EventArgs e)
{
imputThread.Abort();
}
private void bootton1_Click(object sender, EventArgs e)
{
imputThread = new Thread(new ThreadStart(ImputData));
imputThread.Start();
} private void bootton2_Click(object sender, EventArgs e)
{
if(imputThread!=null)
{
imputThread.Abort();
imputThread=null;
}
}这样吧
private void Button1_Click(object sender, System.EventArgs e)
{
imputThread = new System.Threading.Thread(new System.Threading.ThreadStart(ImputData));
imputThread.Start();
}private void Button2_Click(object sender, System.EventArgs e)
{
imputThread.Abort();
}
{
if (imputThread == null)
{
mputThread = new Thread(new ThreadStart(ImputData));
}
imputThread.Start();
}
private void bootton2_Click(object sender, EventArgs e)
{
if(mputThread.IsAlive)
imputThread.Abort();
}
using System.Threading;public class Worker
{
// This method will be called when the thread is started.
public void DoWork()
{
while (!_shouldStop)
{
Console.WriteLine("worker thread: working...");
}
Console.WriteLine("worker thread: terminating gracefully.");
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool _shouldStop;
}public class WorkerThreadExample
{
static void Main()
{
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork); // Start the worker thread.
workerThread.Start();
Console.WriteLine("main thread: Starting worker thread..."); // Loop until worker thread activates.
while (!workerThread.IsAlive); // Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:
Thread.Sleep(1); // Request that the worker thread stop itself:
workerObject.RequestStop(); // Use the Join method to block the current thread
// until the object's thread terminates.
workerThread.Join();
Console.WriteLine("main thread: Worker thread has terminated.");
}
}
{
imputThread.Abort();
imputThread=null;
}
private void bootton1_Click(object sender, EventArgs e)
{
Thread imputThread = new Thread(new ThreadStart(ImputData));
imputThread.Start();
} 如何在函数ImputData()中关闭进程imputThread呢?谢谢各位,此问题解决马上给分。ImputData()
{
//如何关闭正在运行的进程呢?
}
ImputData() 函数中用Thread.CurrentThread就可以当前线程了
}
在需要终止线程的时候,将该全局bool变量置为false就可以正常终止线程并不会有任何意外,因为一个线程的回调函数执行结束就标志该线程正常退出,这个应是一个标准的做法了。这样做的优点是可以正确的获得线程执行后的相应结果,而不必担心中途强制结束线程带来的各种问题。
}
在需要终止线程的时候,将该全局bool变量置为false就可以正常终止线程并不会有任何意外,因为一个线程的回调函数执行结束就标志该线程正常退出,这个应是一个标准的做法了。这样做的优点是可以正确的获得线程执行后的相应结果,而不必担心中途强制结束线程带来的各种问题。
}
在需要终止线程的时候,将该全局bool变量置为false就可以正常终止线程并不会有任何意外,因为一个线程的回调函数执行结束就标志该线程正常退出,这个应是一个标准的做法了。这样做的优点是可以正确的获得线程执行后的相应结果,而不必担心中途强制结束线程带来的各种问题。