-2

I am having a problem handling threads in my code. I want to make a function wait until the button within its own form is clicked. A simple scenario looks like this. This is just an overview of my real problem. Suppose that myFunction() is called from another class and uses its returned value.

public class myForm: Form
{   int x=0;
    public dialogForm()
    {
        InitializeComponent();
    }
    public int myFunction() {
        //do something
        //wait for button1's click
        return x;
    } 
  private void button1_Click(object sender, EventArgs e)
    {
       // using this button to change the value of x
       x=2;
    }

Thanks in advance. Hope anyone helps.

Adnan Temur
  • 39
  • 1
  • 9
  • By their nature, UI controls tell you when the user interacts with them via *events* – Ňɏssa Pøngjǣrdenlarp Dec 17 '18 at 17:30
  • Is myFunction called from a different thread? And does it matter if the button was pressed anytime before myFunction was even called or do you want the function to wait for the next button press? – NineBerry Dec 17 '18 at 17:35
  • This is in general a bad idea, you're much better off when you split the threaded code into two parts and let the button start a thread to execute the 2nd part. Makes it much easier to reason through the threading race problems. Including the one you *must* solve, but haven't considered yet, when the user closes the window while the thread is still running. – Hans Passant Dec 17 '18 at 18:01
  • @NineBerry No there is only the main thread. And I know this is not possible on a single thread. So I need some help out here. – Adnan Temur Dec 18 '18 at 00:55

1 Answers1

1

You can use AutoResetEvent in this case. Below is the updated sample code.

public class myForm: Form
{   
    int x=0;
    AutoResetEvent auto;
    public myForm()
    {
        auto = new AutoResetEvent(false);
        InitializeComponent();
    }
    public int myFunction() {
        //do something
        //wait for button1's click
        auto.WaitOne();
        return x;
    } 
  private void button1_Click(object sender, EventArgs e)
    {
       // using this button to change the value of x
      x=2;
      auto.Set();
    }
}
Nilesh Shinde
  • 380
  • 1
  • 13
  • Thanks for the answer. I tried the above code but the problem still remains. The above method causes the main thread to stop and everything stops. I want to do this in a new thread other than the main thread. The actual scenario is that this form is called from another form handled by the main thread. But displaying a form in a new thread from the main thread seems tricky and I do not know the exact method. I will be glad to hear the solution. – Adnan Temur Dec 17 '18 at 23:49
  • So I think instead of blocking function call, you can use events in C#. Here is sample code shows events between forms. String value from form2 is passed to form1 using event. This will not freeze the UI. You can use the same logic across multiple forms. See if this helps. – Nilesh Shinde Dec 18 '18 at 03:38
  • public delegate void ClickEventHandler(String value); public partial class Form1 : Form { private ClickEventHandler clickHandler; public Form1() { clickHandler += Form2ClickHandler; } private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(clickHandler); frm2.Show(); } private void Form2ClickHandler(String value) { textBox1.Text = value; } } – Nilesh Shinde Dec 18 '18 at 03:42
  • public partial class Form2 : Form { private ClickEventHandler clickHandler; public Form2(ClickEventHandler handler) { clickHandler = handler; InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { clickHandler(textBox1.Text); } } – Nilesh Shinde Dec 18 '18 at 03:42
  • Thanks a lot for the suggestions. Well it seems that I should not mess up with the main UI thread. So this solution seems a better solution. – Adnan Temur Dec 18 '18 at 10:08
  • Glad it helped! Please, consider accepting my answer/voting up. – Nilesh Shinde Dec 19 '18 at 05:29