Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

Having one method wait until a specific event has finished

I have a problem that I have found many similar problems related to multi threading, but noone linked to me specific problem.

I have a method that does something, but when the method has been called, I don't want it to return untill a button is clicked.

So basically, at the end of the method, I would like it to stay put and wait for the Button_Click event to finish, and then the event should in some way "tell" the method to continue.

Currently I have done this by adding a loop at the end of the method like this:

while(someVariable){
  Thread.Sleep(10);
  Application.DoEvents();
}

Then the Button_Click event set someVariable to be false, and then the loop stops.

Of course, this looks very "iffy", and having this loop run every 10 milliseconds seems like a gigantic waste.

Are there any way of doing this properly? Since the method in question runs on the same thread as the rest of the application, it's also important that halting the method does not block the tread for other activities.

The reason I need this is because when the method is called from somewhere else, this method will draw up some components (including two buttons). Then the user clicks one of them, and then the method will return a different value depending on which button was clicked, and the program calling it can't continue untill it knows which button was clicked. So my program will look like this.

....
if( someMethod() == ButtonA ){
  //do the proper action if button 1 is clicked
}else{
  //do the proper action if button 2 is clicked
}

I hope this was not to confusing.

Upvotes: 1

Views: 7620

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1499740

Assuming this is in the UI thread, you absolutely should not do this. You say "it's important that halting the method does not block the thread for other activities" - that basically means you need to find a different design. If the UI thread is "staying put and waiting for the Button_Click event to finish" then it can't be doing anything else without using the hack of Application.DoEvents().

Instead, you should make a call back to the original caller when the event occurs. Either expose an event that the caller can subscribe to, and raise that event when the button click finishes, or get the caller to pass in a delegate to call appropriately. (They're equivalent really, just different ways of propagating the callback.)

That's the way WinForms (and other rich client UIs) are designed to work - on an event-based model, basically.

Upvotes: 4

Andreas Paulsson
Andreas Paulsson

Reputation: 7803

Use AutoResetEvent, see sample and documentation at http://msdn.microsoft.com/en-us/library/58195swd.aspx

Your main thread does a WaitOne() on the event and in you Button.Click event, you Set() the event.

Upvotes: 0

Related Questions