title: Wait for method to finish an AutoResetEvent without blocking UI
body: <p>Sorry if this is quite similar to other questions, but I just can't make this work..</p>
  
  <p>How do I run this code, without blocking my ui when calling <code>wh.WaitOne()</code>?</p>
  
  <pre><code>public partial class Form1 : Form
  {
      private readonly AutoResetEvent wh = new AutoResetEvent(false);
  
      public void button1_Click(object sender, EventArgs e)
      {
          //Some work
          MessageBox.Show("Before pause");
  
          string someVar = activate();
  
          MessageBox.Show("After pause");
          //some other work which should only run when 'string someVar = activate();' above succeeds
      }
  
      private string activate()
      {
          wh.WaitOne();
          return textBox1.Text;
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          wh.Set();
      }
  }
  </code></pre>
  
  <p>I know I could put the <code>wh.WaitOne()</code> inside a new thread, but <code>return textbox1.Text</code> would get executed right after the thread started without waiting for it to finish. Is there a simple way of waiting for the thread containing <code>wh.WaitOne()</code> to finish perhaps?</p>