简体   繁体   中英

C# WinForms application: stop / interrupting a Windows Forms application

Environment: .net 3.5 WinForms application

I am looking for some ideas how to stop / interrupting a Windows Form application, eg when “something” takes too long. 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

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. 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. It has a CancelAsync() method, explicitly designed to do what you want to do. Be sure to review the code sample provided in the MSDN Library article for this method.

Execute the long running code in a separate 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. 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.

You may consider using the BackgroundWorker class, it abstracts out working directly with threads, so is a little easier to implement. You just create an instance of BackgroundWorker and attach some event handlers to it. Here is an example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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