title: using ManualResetEvent() in while() loop
body: <p>i have a question about my simply code:</p>
  
  <pre><code> public partial class Form1 : Form {
  
    ManualResetEvent ResetEvt = new ManualResetEvent(false);
    Thread t1;
    public Form1() {
       InitializeComponent();
    }
    //create
    private void button1_Click(object sender, EventArgs e) {
       t1 = new Thread(new ParameterizedThreadStart((reset) =&gt; {
          int cont = 0;
          var resett = reset as ManualResetEvent;
          Thread.CurrentThread.IsBackground = true;
          while (resett.WaitOne()) {
             try {
                this.BeginInvoke(new Action(() =&gt; this.richTextBox1.AppendText("Ciao " + cont)));
                cont++;
                Thread.Sleep(500);
             }
             catch (Exception) {
                MessageBox.Show("END");
             }
          }
          this.BeginInvoke(new Action(() =&gt; this.richTextBox1.AppendText("ok finished " + cont)));
       }));
    }
  
    private void button2_Click(object sender, EventArgs e) {
  
       try {
          t1.Start(this.ResetEvt);
       }
       catch (Exception) {
  
       }
    }
  
    private void button3_Click(object sender, EventArgs e) {
       this.ResetEvt.Reset();
    }
  
    private void button4_Click(object sender, EventArgs e) {
       ResetEvt.Set();
    }
  
    private void button5_Click(object sender, EventArgs e) {
       // ??? ho can stop while? and continue execution?
    }
  </code></pre>
  
  <p>}</p>
  
  <p>Weel, how to use ManualResetEvent is clear and simple, but why the line
   <code>this.BeginInvoke(new Action(() =&gt; this.richTextBox1.AppendText("ok finished " + cont)));</code> will be never executed? I can stop my thread (.Reset) and restart it (.Set), but when resett.WaitOne() became FALSE why my code don't jump to <code>this.BeginInvoke(new Action(() =&gt; this.richTextBox1.AppendText("ok finished " + cont)));</code>?</p>
  
  <p>thanks a lot.</p>