Stop a loop with a button - I need threads?

I have a two buttons in a form: START and STOP. The START button starts a function with a loop inside (this loop could take a lot of time to be concluded) and after that calls an other function for data post-processing. I want to be able of stop and exit the loop pressing the STOP button. I have already a boolean to check if STOP was pressed, but the problem is that the system doesn't recognize button events while the loop is running. I have to start a thread for the loop function? If so, the post-processing function will be called only at the end of the loop (if STOP was not pressed)? Thank you in advance.
You could periodically handle events. Other than that, yes, you'll have to use threads.
Thank you, Helios. How can I make that events handling in my case?
You'd better use a thread.
As the function hasn't returned,
the UI thread won't process the Stop button's message.
Event maybe not solve you question,
as the stop message can't be processed if the function not returned.
Thank you, IcerLion. At this moment I have started a thread to run my loop function. I'm expecting that the STOP button click was percieved by the application, my 'should_stop' boolean was updated to 'true' and I could 'break' my loop and exit that function. Unfortunatly, it is no so. Despite I have a thread running the loop function the system seems to doesn't handle the click button event... What could be missing in my approach? Thanks. Joao
You can even get the input inside that loop and change that bool variable inside the loop when you press that thing.
Yes, but how can I get the input (button click) inside the loop, Jones? Thanks.
EDIT: The principle applies to UNIX/Linux, but the mentioned functions are Windows-only.

To signal a thread you better use an event.

When the start button is depressed, use CreateEvent() to create a new event. Pass this event as part of the data that you pass to the new thread. I recommend that you declare a suitable struct for this.

Inside the worker thread, use WaitForSingleObject() with a timeout of zero. If the function returns WAIT_OBJECT_0, the thread has been signaled to stop.

On depress of the Stop button, use SetEvent() to signal de stop event.

Finally, if you must synchronize the user interface, one Stop use WaitForSingleObject() to wait for the worker thread to finish, then update the UI as needed.
Last edited on
Many thanks for your reply, webJose.
In fact I have tried to follow your suggestion. Unfortunatly I can't get it work yet.

Inside 'StopButton_Click' function I have created and set the event:

1
2
 hEvent = CreateEvent ( NULL , false , false , "MyEvent" );
 SetEvent(hEvent);


In my threaded function (the loop function) I open the event, check if the event was setted and finally close the event handler. Something like this:

1
2
3
4
5
6
7
8
HANDLE hEvent_ = OpenEvent ( EVENT_ALL_ACCESS , false, "MyEvent" );

loop{
	if (WaitForSingleObject(hEvent_, 0) == WAIT_OBJECT_0)
		break;
}

CloseHandle(hEvent_);


Do you have any ideia about the reason the application can't listen to STOP button click?
Thank you.
Joao
Topic archived. No new replies allowed.