2011-06-08 43 views
0

我在應用程序中使用的WinForms DataGridView的UI不會更新

1) Parallel.ForEach(linklist, x => DownloadAsync(x)); 
2) webClient.DownloadStringAsync(new Uri(link.Url)); 

class Link 
{ 
string url; 
string result; 
int progress; 
} 

其下載工作,並更新使用INotifyPropertyChanged的DataGridView的。

問題是,下載速度很快,datagridview更新第一行,然後UI不更新,但隨後將鼠標光標逐一移動到datagridview行上,每行更新值。

我不明白我失去了什麼。

請任何建議,謝謝你提前。

編輯:異步不阻止的GUI,所以我不使用後臺線程。

+0

因此,您已將列表設置爲DataGridView的DataSource,並且列表是ObservableCollection? – 2011-06-08 04:56:06

+0

BindingSource是DataSource,它獲取LinkList – 2011-06-08 04:58:49

回答

3

雖然你不顯示你的代碼,但我認爲你正在從backgroud線程更新UI元素,這是你永遠不應該做的事情。如果您的目標是WinForms,則需要使用BackgroundWorkerControl.Invoke

BackgroundWorker Component Overview
How to: Implement a Form That Uses a Background Operation
How can I update my user interface from a thread that did not create it?

+0

異步不會阻止GUI,不我不使用backgroundworker,同一個線程 – 2011-06-08 05:00:13

+0

@Milan Aditya - 正確,異步不會阻止GUI,但是當異步回調時它會回調非回調-UI線程,對吧?所以你從非UI線程更新datagrid數據源。 BackgroundWorker保證回調會在UI線程中發生。 – 2011-06-08 05:02:42

+0

是的,你是正確的,但我正在做的是我已經設置dataGridView1.DataSource = _BindingSource;在form_load上,它會自動使用INotifyPropertyChanged更新 – 2011-06-08 05:14:20

-1

你的對象,約束,需要實現INotifyPropertyChanged