用户能够取消该过程

编程入门 行业动态 更新时间:2024-10-07 22:27:12
本文介绍了用户能够取消该过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

推荐答案

看看 BackgroundWorker类 [ ^ ].您可以使用后台工作程序执行您的逻辑,并通过调用 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(); } }

更多推荐

用户能够取消该过程

本文发布于:2023-11-03 04:58:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过程   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!