4

Ive got a thread that does some work in the background and passes updates to the Form using the Invoke, BeginInvoke methods. The thread is created after the form is displayed so no issue there.

The issue is how to correctly shutdown. My worker thread has the ability to be asked to exit, and will exit some time soon after that (miliseconds).

However if the form has closed first the Invoke stuff breaks. I tried adding a Thread.Join to the forms closing event, but of course this causes a deadlock, even for BeginInvoke since somhow a Thread.Join blocks a BeginInvoke on that thread...

What is the correct way to close the form and shutdown its worker thread cleanly?

EDIT:

basic current code:

volatile bool abort;
void WorkerThread()
{
    while(!abort)DoStuffIncludingInvokesOnThisForm();
}
void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    abort = true;
    workerThread.Join();//will deadlock with DoStuffIncludingInvokesOnThisForm
    //if get here before workerThread has exited, invokes will fail if workerthread is still in DoStuffIncludingInvokesOnThisForm
}
Will
  • 101
  • 1
  • 3

2 Answers2

1

Add a handler to FormClosing and ask your thread to exit nicely...

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Well I already have logic to ask it to exit, and as I said it exits within miliseconds of being asked to, but how keep the form alive until it has, when using Thread.Join causes all the invokes to block so the worker thread gets stuck in one. – Will Jul 27 '11 at 22:05
  • Will, can't you just wait for the Thread to exit doing some sort of loop inside FormClosing for example: while(NastyThread.isAlive)Thread.Sleep(100);? – Icarus Jul 27 '11 at 22:19
  • @Icarus: No, that would be just as bad as calling `Thread.Join`. It still blocks the UI thread. – Brian Gideon Jul 27 '11 at 22:43
1

From the Form.Closing event handler set the Cancel property to true to temporarily defer closing of the form until the worker thread has finished. Once the worker thread has terminated then you can reissue the Close command on the form.

public class MyForm : Form
{
  private volatile bool abort = false;
  private bool IsCloseRequested = false;
  private bool IsWorkerThreadComplete = false;

  private void MyForm_Closing(object sender, FormClosingEventArgs args)
  {
    if (!IsWorkerThreadComplete)
    {
      args.Cancel = true;
      IsCloseRequested = true;
      abort = true;
    }
  }

  void WorkerThread()
  {
    try
    {    
      while (!abort) DoStuffIncludingInvokesOnThisForm();
    }
    finally
    {
      OnWorkerThreadComplete();
    }
  }

  private void OnWorkerThreadComplete()
  {
    if (InvokeRequired) 
    {
      Invoke(((MethodInvoker)() => OnWorkerThreadComplete), null);
    }
    else
    {
      IsWorkerThreadComplete = true;
      if (IsCloseRequested) Close();
    }
  }
}
Brian Gideon
  • 47,849
  • 13
  • 107
  • 150