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 :)

Recommended Answers

All 5 Replies

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.

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.

commented: Exactly what I do. +10

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

Hey :)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.