954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Measurement Program Abort Button

Hey guys,

I created a main GUI for a measurement program. Beside all existing controls, there are two important buttons. START and ABORT.

By pressing the START button, a method for measuring is run. Now I can not press any control on the form. That's okay, but I need the ABORT button and just the abort button to work.

Do you have any idea to solve my problem.
Thanks a lot :)

Tortura
Newbie Poster
15 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

I believe you can use this:

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx

To help you. Basically you run the desired code in the background, using the method

Application.DoEvents();


Hope it's what you're looking for.

charlybones
Junior Poster in Training
77 posts since Jan 2011
Reputation Points: 16
Solved Threads: 16
 

Sounds like you should look into multithreading. Running the UI on one thread and the background work on one or more threads is fairly common practice these days.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

A bit of clarification: In a single-threaded application, the main thread handles updating the forms and other user interface elements. If you simply put a long-running task in a button's click handler, the main thread stays busy there, and doesn't get a chance to update the UI until it's done. This is why you can't do anything on the form once it starts. When you call DoEvents on the main thread, it processes any pending messages to the application, so the UI remains responsive. The trick is you have to call it repeatedly while your task is running.

So. You could sprinkle your "start" task with calls to DoEvents, have the "abort" handler set some Boolean variable when clicked, and have your "start" task check this variable periodically and stop when it's set.

If you search the Internet, you'll find a variety of opinions about DoEvents. Here's mine: It's fine for simple tasks you want to happen in the main thread, and lets you avoid the complexity of multithreading. As your tasks get longer and more involved, though, it can get ugly.

I don't use DoEvents, and I don't recommend it to anyone, either. Instead, consider the BackgroundWorker class. All you have to do is add an event handler to do your work, and this class takes care of the multithreading for you. To abort, you would use the CancelAsync method; your "do work" event handler has to check the CancellationPending property to know when to abort. This is no more complicated than what you'd have to do with the DoEvents method outlined above, and I believe it's better because it keeps UI business out of your internal code.

gusano79
Posting Pro
521 posts since May 2004
Reputation Points: 182
Solved Threads: 77
 

Thank you for now.
I will try to manage it with your answers and tell you if it works.

Tortura
Newbie Poster
15 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Hey :)

Today I tried to solve my problem with the BackgroundWorker...and with some little problems it works. Thank you for the hint :)

Tortura
Newbie Poster
15 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: