我一直在寻找这样的例子:
https://msdn.microsoft.com/en-us/library/edzehd2t%28v=vs.110%29.aspx
并且遍布stackoverflow但是我无法让它工作或找到答案。或者答案不适用于我正在做的事情,或者答案中没有足够的信息让我理解如何使这项工作。
我正在尝试编写好的代码并避免使用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交互?
答案 0 :(得分:0)
尝试使用http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Security.html#VPC_Security_Comparison。您可以在RunWorkerCompleted
活动中更新您的用户界面。