在發佈lambda表達式到當前WindowsFormsSynchronizationContext
,我發現拉姆達代碼執行在後臺線程:WindowsFormsSynchronizationContext.Current.Post後臺線程執行拉姆達
// running on main thread here
myLabel = new Label();
this.Controls.Add(myLabel);
WindowsFormsSynchronizationContext.Current.Post(ignore => {
// returns true !
bool invokeRequired = myLabel.InvokeRequired;
// returns a background thread, not the UI thread
int threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
// throws, because we are (unexpectedly) on a background, different thread
myLabel.Text = "whatever";
},null);
此外,WindowsFormsSynchronizationContext.Current
似乎並沒有返回一個WindowsFormsSynchronizationContext
,而是一個普通的System.Threading.SynchronizationContext
。
這突然發生的事情是以前有沒有線程的問題,並沒有被最近修改(解決方案的其他部分都)在窗體上。我試圖尋找明顯的錯誤(例如,在後臺線程上實例化窗體本身的代碼或在後臺線程上創建的控件),但我一直未能找到明顯的違規行爲。
也許我在找錯方向?
'WindowsFormsSynchronizationContext.Current'將返回null從另一個線程查詢時,你必須將其保存以備後用 –
不是null;投射時它是空的。我會澄清,謝謝@Sriram。 – magma
這將是空的,請澄清 –