Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1103669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:22:25+00:00 2026-05-17T01:22:25+00:00

I have a problem that I have found many similar problems related to multi

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:22:25+00:00Added an answer on May 17, 2026 at 1:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a hum-dinger of a problem that I have not found an answer
I have the problem that an specific step in Ant can only be executed
I have a problem that confuses my users, being that although an item is
I have a problem that I would like have solved via a SQL query.
I have a problem that I feel is best implimented in a stand alone
I have a problem that seems like its a result of a deadlock-situation. Whe
I have the odd problem that I am not able to open the properties
I have encountered a problem that I have not come accross yet when setting
I have a tricky problem that I've been messing about with for a few
I have a problem with the jquery-ui dialog box . The problem is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.