C#Threading Run and Cancel按钮,需要能够取消长时间的处理运行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#Threading Run and Cancel按钮,需要能够取消长时间的处理运行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2913字,纯文字阅读大概需要5分钟。
内容图文
![C#Threading Run and Cancel按钮,需要能够取消长时间的处理运行](/upload/InfoBanner/zyjiaocheng/656/e985a23e0e0246d5a74041b65e85d63d.jpg)
当用户单击“运行”时,该应用程序将运行大量代码以生成模型并将其显示在图表中.运行大约需要1-2分钟才能运行.我还有一个“取消”按钮,在单击“运行”按钮后会启用.我正在使用DotSpatial,因此我的按钮位于功能区UI的插件面板上. Run and Cancel上的click事件从插件开始,它调用后端类的代码Run and Click.
当用户在运行开始后单击cancel时,会有一个延迟,但是cancel方法会被调用并执行,但是运行永远不会停止,最终我们会看到图表显示.因此,我认为我需要一个单独的线程来运行.我对编程还很陌生,并且从未使用过线程.我调查了一下并添加了以下代码,但是我的线程方法未运行.这是我的代码:
单击运行按钮:
这是顶部:
//check to see if RunModel thread needs to stop or continue
private volatile bool stopRun = false;
private Thread runThread;
然后,这是从click事件调用的方法:
public void btnRun_testingThread(object sender, EventArgs e)
{
//create a new thread to run the RunModel
if (runThread == null)
{
//we don't want to stop this thread
stopRun = false;
runThread = new Thread(RunModel);
runThread.Start(); <--this isn't doing anything
}
因此,我认为当代码到达runThread.Start()时,它将跳入我的RunModel方法并开始运行代码.但事实并非如此.另外,我想取消这个线程(一旦我让它正常工作),所以我有了这个,它可以通过cancel click方法调用:
private void StopRunThread()
{
if (runThread != null)
{
//we want to stop the thread
stopRun = true;
//gracefully pause until the thread exits
runThread.Join();
runThread = null;
}
}
然后这是RunModel(),我偶尔在其中检查一下stopRun布尔值是否已更改.
public void RunModel()
{
...some code.....
//check to see if cancel was clicked
if (stopRun)
{
....clean up code....
return;
}
....some more code....
//check to see if cancel was clicked
if (stopRun)
{
....clean up code....
return;
}
}
以及取消按钮的单击方法:
public void btnCancel_Click(Object sender, EventArgs e)
{
stopRun = true;
StopRunThread();
//the model run has been canceled
....some code.....
}
对获取thread.start实际运行Run方法有任何帮助吗?然后,我是否需要在运行中经常检查挥发物,以清除一切(如果已停止)?谢谢!
解决方法:
我认为您最好看看BackgroundWorker-它本质上是独立运行的,但可以提防取消命令.确保在初始化时添加“ WorkerSupportCancellation”:
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); // This does the job ...
backgroundWorker1.WorkerSupportsCancellation = true; // This allows cancellation.
然后单击即可开始您的过程:
public void btnRun_testingThread(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
您的取消按钮可以发出取消请求:
public void btnCancel_Click(Object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
然后,您的工作人员就可以在工作过程中对其进行监视…
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
if (backgroundWorker1.CancellationPending)
{
break;
}
else
{
// Do whatever you're doing.
}
}
e.Result = backgroundWorker1.CancellationPending ? null : orders;
}
您可以通过添加进度条等来进一步增强此功能,但这会变得更加复杂,因此在此不再赘述.
内容总结
以上是互联网集市为您收集整理的C#Threading Run and Cancel按钮,需要能够取消长时间的处理运行全部内容,希望文章能够帮你解决C#Threading Run and Cancel按钮,需要能够取消长时间的处理运行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。