WinForms multithreading issue

六眼飞鱼酱① 提交于 2019-12-21 19:24:42

问题


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
}

回答1:


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




回答2:


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();
    }
  }
}


来源:https://stackoverflow.com/questions/6851713/winforms-multithreading-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!