views:

94

answers:

4

Environment: .net 3.5 WinForms application

I am looking for some ideas how to stop / interrupting a Windows Form application, e.g. 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

+1  A: 

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();

Jesper Palm
Thanks for your help, I really appreciate it. Please read the comment at Hans' answer.
Horst Walter
+1  A: 

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.

Floste
Thanks for your help, I really appreciate it. Please read the comment at Hans' answer.
Horst Walter
+2  A: 

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.

Hans Passant
Thanks to all who have helped. Basically all of you have had the same approach, unfortunately my "reputation" is not good to give credits to all of you....
Horst Walter
Okay, I took care of it.
Hans Passant
Thanks Hans, for both ;-)
Horst Walter
+1  A: 

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.

Matt
Thanks for your help, I really appreciate it. Please read the comment at Hans' answer.
Horst Walter