用户能够取消该过程 [英] user be able to cancle the process

查看:85
本文介绍了用户能够取消该过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的项目在处理时会花费一些时间.我希望用户能够通过单击按钮来取消该过程.我该怎么做?

Hi,The project I wrote takes time when processing.I want the user to be able to cancle the process by clicking a button.How can I do that? Thanks in advanve.

推荐答案

看看 CancelAsync [
Have a look at the BackgroundWorker Class[^]. You could execute your logic using the background worker and offer the user a possibility to cancel the execution by calling CancelAsync [^] in the button.


这取决于您的操作方式.
如果您在UI线程中运行了很长的操作(即您尚未将其移动到单独的线程中),那么它将阻止用户界面执行任何操作-因此它无法响应您的鼠标单击或按下按钮.有一种方法可以通过调用Application.DoEvents来解决,但这是一个非常讨厌的解决方案,如果调用频率不够高,可能会使响应变慢,或者如果调用频率太高,则会使处理时间更长.

更好的解决方案是将长时间操作放在单独的线程中-查看
BackgroundWorker类 [^ ]-非常简单,该链接包含带有取消按钮的示例.

如果您已经拥有一个单独的任务,那么只需取消操作即可!
It depends on how you are doing it.
If you have the long operation running in the UI thread (ie, you haven''t moved it to a separate thread already) then it will block the user interface from doing anything - so it can''t respond to you mouse clicks or button presses. There is a way round that, by calling Application.DoEvents, but it is a very nasty solution, and can make response sluggish if you don''t call it often enough, or make the processing much much longer if you call it too often.

A much better solution is to put the long operation in a separate thread - look at the BackgroundWorker class[^] - it''s pretty simple, and the link includes a sample with a cancel button.

If you already have it if a separate task, then just cancel the operation!


您还可以使用
You can also use
private void Buttoncancel_Click(object sender, System.EventArgs e)
{
   System.Diagnostics.Process[] myProcesses;
   myProcesses =
      System.Diagnostics.Process.GetProcessesByName("ProcessName");
   foreach (System.Diagnostics.Process instance in myProcesses)
   {
      instance.CloseMainWindow();
   }
}


这篇关于用户能够取消该过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆