Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从后台线程正确引发事件,并在UI线程中处理_C#_Event Handling_Thread Safety - Fatal编程技术网

C#从后台线程正确引发事件,并在UI线程中处理

C#从后台线程正确引发事件,并在UI线程中处理,c#,event-handling,thread-safety,C#,Event Handling,Thread Safety,我一直在网上寻找这样的例子: 到处都是,但我不能让它工作或找到答案。或者答案不适用于我正在做的事情,或者答案中没有足够的信息让我理解如何使这项工作起作用 我试图在这里编写好代码,避免Application.DoEvents()。看看代码 private void btnSendQ_Click(object sender, EventArgs e) { //launch a thread from a button click event Thread ManualQuery

我一直在网上寻找这样的例子:

到处都是,但我不能让它工作或找到答案。或者答案不适用于我正在做的事情,或者答案中没有足够的信息让我理解如何使这项工作起作用

我试图在这里编写好代码,避免Application.DoEvents()。看看代码

private void btnSendQ_Click(object sender, EventArgs e)
{
     //launch a thread from a button click event
     Thread ManualQueryThread = new Thread(StartManualQueryThread_Thread);
     ManualQueryThread.Priority = ThreadPriority.Normal;
     ManualQueryThread.IsBackground = true;
     Platform.Log(LogLevel.Debug, "Starting Manual Query Thread.");
     Stats.g_CurrentStatus = "Starting Manual Query Thread.";
     ManualQueryThread.Start();

}

private void StartManualQueryThread_Thread()
{
     try
     {
        //Here work is performed in the background thread populating objects with data.
     }
     catch (Exception e)
     {
          Platform.Log(LogLevel.Error, "Error in StartManualQueryThread(). The error is: " + e.ToString());
     }
     finally
     {
          //raise event here since the thread has completed
          OnC_FindComplete(EventArgs.Empty);                    
     }
}

    public event EventHandler C_FindComplete;

    protected virtual void OnC_FindComplete(EventArgs e)
    {
        //display the gathered information in a datagrid using DisplayC_FindResults(), and enable UI objects.
        EventHandler handler = C_FindComplete;        
        DisplayC_FindResults();
        btnSendQ.Enabled = true;
        btnMove.Enabled = true;
    }

问题是,在创建发布版本时,datagrid会收到一个空引用异常,并执行红色X操作,因为它仍在由后台线程更新。那么,如何在后台线程中正确引发事件,并在UI线程中处理该事件以与UI交互?

尝试使用。您将能够在
RunWorkerCompleted
事件中更新您的UI。

避免重新发明轮子,将BackgroundWorker从工具箱放到表单上。它的RunWorkerCompleted事件是在UI线程上引发的,您不会得到失败的红十字。@Svyatski是的,这很有效。我有个问题。这比我尝试的方法更可取、更优越吗?有太多的事情要做,就是提出事件并处理它们。事实上,我问了一个java开发人员这个问题,他曾经是一个.Net开发人员,在今天的工作中工作了很多年,我得到的答案是引发事件并处理它们。@Jake当你使用Windows窗体生成后台线程并希望从它们返回一些结果到你的UI时,你最好还是和幕后工作者呆在一起。您当然可以使用.NET4.0+中的线程和任务,但所有同步都必须由您自己完成。