我有一个类,它有一些创建和发生的任务。当我运行测试应用程序时,创建/使用的线程示例如下所示.
我能够将一个对象从我的主线程(9)传递到工作线程(10),并从工作线程(10)传递到辅助线程(11) ContinueWith线程(11) AKA工作线程(10)的完成。我做的每件事都很好,只有一个例外.我似乎无法使用类似于事件的东西将该对象返回到主线程。每当我尝试将委托或调用返回到主线程时,它将无法工作。它总是在Thread11而不是Thread9上触发!
我的目标是将对象(在Thread11中使用)返回到主Thread9。
备注:
更新-实例:
private SQLConnectionProperties sql = new SQLConnectionProperties();
private void SQLTest()
{
//Being called from main UI thread 9
sql.SQLErrorMessage = "FIRST CHANGE";
Task<SQLConnectionProperties> taskConnect = Task.Factory
.StartNew(() => threadConnect(sql), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default)
.ContinueWith((task) => threadConnect(task.Result, true));
//Continue back to main UI (still in main UI thread 9)
}
private SQLConnectionProperties threadConnect(SQLConnectionProperties sql, bool bContinuation = false)
{
if (!bContinuation)
{
//In new thread 10
sql.SQLErrorMessage = "SECOND CHANGE";
}
else
{
//In new thread 11
sql.SQLErrorMessage = "THIRD CHANGE";
}
return sql;
}使用上面的代码,我可以得到我想要的所有东西,直到In new thread 11行。在将SQLErrorMessage更改为“第三个更改”并返回之后,我需要引发某种类型的事件,或者将结果返回到主UI线程9。
发布于 2015-06-11 16:55:31
正如@r00tdev所解释的,使用SynchronizationContext()类是我想要做的事情的解决方案。下面,您将看到一个示例,说明我如何使用它将工作线程的结果返回到主UI线程。谢谢你的回答@r00tdev!
示例:
private void SQLTest()
{
//Set up a sync context back to this main UI thread
m_sql.SyncContext = SynchronizationContext.Current;
//Being called from main UI thread 9
m_sql.SQLErrorMessage = "FIRST CHANGE";
Task<SQLConnectionProperties> taskConnect = Task.Factory
.StartNew(() => threadConnect(m_sql), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default)
.ContinueWith((task) => threadConnect(task.Result, true));
//Continue back to main UI (still in main UI thread 9)
}
private SQLConnectionProperties threadConnect(SQLConnectionProperties sql, bool bContinuation = false)
{
if (!bContinuation)
{
//In new thread 10
sql.SQLErrorMessage = "SECOND CHANGE";
}
else
{
//In new thread 11
sql.SQLErrorMessage = "THIRD CHANGE";
sql.SyncContext.Post(threadConnectComplete, sql);
}
return sql;
}
private void threadConnectComplete(object state)
{
//Back in the main UI thread! Save the changes to the sql object
SQLConnectionProperties sql = state as SQLConnectionProperties;
m_sql = sql;
//See the results and use this area to update the main UI
Debug.WriteLine("SQLErrorMessage:" + sql.SQLErrorMessage);
//TESTING NON THREAD SAFE just to show you no runtime errors happen
form_MainUI.textbox1.Text = sql.SQLErrorMessage;
}发布于 2015-06-11 04:01:50
您需要使用_synchronizationContext.Send或_synchronizationContext.Post与UI线程进行通信。
另外,从您提到的内容来看,我认为在调用上述任何一个函数时,还需要传递"false“,这样它就知道该函数不是从UI线程调用的。
_synchronizationContext = SynchronizationContext.Currenthttps://stackoverflow.com/questions/30770788
复制相似问题