简体   繁体   English

C#WinForms应用程序:停止/中断Windows窗体应用程序

[英]C# WinForms application: stop / interrupting a Windows Forms application

Environment: .net 3.5 WinForms application 环境:.net 3.5 WinForms应用程序

I am looking for some ideas how to stop / interrupting a Windows Form application, eg when “something” takes too long. 我正在寻找一些如何停止/中断Windows窗体应用程序的想法,例如当“某事”需要太长时间时。 A typical scenario is reading large amount of data from a backend system and the user shall be able to press some key immediately stopping the loading. 典型的情况是从后端系统读取大量数据,用户应能够立即按下一些键来停止加载。

  1. Normal events are not responsive enough, since the backend call somehow consumes so much time, they are not reacting in promptly fashion. 正常事件没有足够的响应,因为后端调用以某种方式消耗了这么多时间,他们没有及时做出反应。
  2. In my read methods I have added explicit checks – but I cannot add such checks in 3rd party frameworks. 在我的阅读方法中,我添加了显式检查 - 但我无法在第三方框架中添加此类检查。 Also it is somehow not a very nice approach. 它也不是一个非常好的方法。

Maybe someone has an idea how to reliable and promptly stop some running methods. 也许有人知道如何可靠并迅速停止一些运行方法。 Is there an interrupt I can use? 我可以使用中断吗?

Regards HW 关心HW

Normal events are not responsive enough, since the backend call somehow consumes so much time 正常事件响应不够,因为后端调用会以某种方式消耗这么多时间

That's not how it works. 这不是它的工作原理。 Understand the "message loop" is pretty important, be sure to read up on it. 理解“消息循环”非常重要,请务必阅读它。 In a nutshell, Windows can only tell your program that a button was clicked when the main thread of program is idle. 简而言之,Windows只能告诉程序在程序的主线程空闲时单击了一个按钮。 Your main thread should always be idle, ready to jump into action when some bit of work needs to be done. 您的主线程应始终处于空闲状态,以便在需要完成某些工作时立即采取行动。 Like painting your form or processing a keystroke. 喜欢画你的表格或处理击键。 Or doing something when the user clicks the Cancel button. 或者在用户单击“取消”按钮时执行某些操作。

Which means that something else needs to do the heavy lifting. 这意味着其他东西需要做繁重的工作。 A thread. 一个帖子。 By far the best way to get started in the troublesome world of threading is the BackgroundWorker class. 到目前为止,开始处理棘手的线程世界的最佳方法是BackgroundWorker类。 It has a CancelAsync() method, explicitly designed to do what you want to do. 它有一个CancelAsync()方法,明确设计用于执行您想要执行的操作。 Be sure to review the code sample provided in the MSDN Library article for this method. 请务必查看MSDN Library文章中为此方法提供的代码示例。

Execute the long running code in a separate Thread. 在单独的Thread中执行长时间运行的代码。

Thread thread;
void StartLong()
{
  thread = new Thread(SomeMethod);
  thread.Start();
}

void SomeMethod()
{
  //Long running code here.
}

void CancelButton_Click(object sender, EventArgs e)
{
  //...
  thread.Abort();
}

If you don't have control over the code that takes long to execute. 如果您无法控制执行时间很长的代码。 All you can do do cancel it is to abort the thread. 你所能做的就是取消它就是中止线程。 thread.Abort();

You should do "long" =eveything longer than 1/5sec operations in seperate threads. 您应该在单独的线程中执行“long”=超过1/5秒操作的时间。 The best way of topping an operation is giving it some kind of "context" with a variable wich tells the workerthread wether the user wants to abbord the operation or not. 打顶一个操作的最好方法是给它一些带有变量的“上下文”,告诉用户想要操作或不操作的操作。
If you have no control over the code doing the operation and the code is .net you may call Thread.Abort() but if the code is not .net the only thing you can do is callinfg the windows api function "terminatethread" but you may get some resource leaks and files not beeing closed so you should think of using a childprocess. 如果您无法控制执行该操作的代码并且代码是.net,您可以调用Thread.Abort(),但如果代码不是.net,您唯一能做的就是调用windows api函数“terminatethread”但是你可能会有一些资源泄漏和文件没有关闭所以你应该考虑使用子进程。

You may consider using the BackgroundWorker class, it abstracts out working directly with threads, so is a little easier to implement. 您可以考虑使用BackgroundWorker类,它直接使用线程进行抽象,因此更容易实现。 You just create an instance of BackgroundWorker and attach some event handlers to it. 您只需创建BackgroundWorker的实例并将一些事件处理程序附加到它。 Here is an example . 这是一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM