目標:跨線程問題更新數據源
一旦我在我的用戶界面上的啓動按鈕點擊,我目前火兩個背景的工人。
BGWorker 1是負責調用的.dll方法抓取網站 BGWorker 2負責更新使用數據源=源設置器
問題在DataGridView:
我的DLL接收作爲參數,這是對.dll運行時將填充的數據源的引用。所以基本上,.dll會偶爾在數據源中添加一個對象,直到它完成運行。
的BGWorker 2執行此:
while (1 == 1) // Runs until is it manually disposed
{
CoursesGrid.BeginInvoke // inline method
(
(Action) delegate
{
if (_coursesSource.Count > 0) // if source is not empty,Binds.
{
try
{
CoursesGrid.DataSource = _coursesSource;
CoursesGrid.EndEdit();
}
catch (Exception ex)
{
Logs.LogWriter.LogError(ex);
}
}
else
{
// Signals User To keep Waiting
}
}
);
Thread.Sleep(4000);
行爲流量:
- DLL被調用
- 方法分析一個元件,並且增加了源
- 最終,BGWorker 2喚醒並將DataGridView源綁定到修改後的源代碼
- DLL的螺紋一直解析但一旦它試圖將新元素添加到源
TL失敗:DR: 線程1上寫入源 線程2讀該修改的源,並將其綁定到DataGridView中它會刷新 線程1失敗源上寫一遍:
"Cross-thread operation not valid: Control 'CoursesGrid' accessed from a thread other than the thread it was created on."}
兩個線程都處理相同來源,但BGWorker 2,只有它綁定到接口,這應該是工作。想知道這裏會發生什麼?
我在猜測某些東西不能按照你想要的方式工作,也許你可以告訴我們什麼是或者不在工作以及錯誤是什麼。 – CodingGorilla
btw,fyi,在C#3.0中,您可以編寫「BeginInvoke((=){...})' – abatishchev
@CodingGorilla編輯。對不起,如果它不是第一次清楚。 增加了一個TL:DR –