Updating DataGrid From a BackGroundWorker

拥有回忆 提交于 2019-12-11 17:53:13

问题


I hava a Background Worker and a DataGrid in my c# Application. In do work of my Backgroundworker which will call an Api in my dlls which will enter some Data in a SQLite Database. After the Completion of my Api call I report a progress and In progress event of my Backgroundworker I get the contents from Db and assign it as a DataSource to my grid. I call same API in same backgroundworker. In the middle of processing my application crashes. But If I dont assign the dataSource in ProgressChanged my application doesnt crashes.


回答1:


I am assuming you must be accessing UI object using Invoke method.

If not try using following approach (Executes the specified delegate, on the thread that owns the control's underlying window handle, with the specified list of arguments.):


 //In Form.Designer.cs

 Label myLabel = new Label();


 //In code behind under Background worker method
 LabelVlaueSetter SetLabelTextDel = SetLabelText; 
 if (myLabel .InvokeRequired)
 {

   myLabel.Invoke(SetLabelTextDel, "Some Value");
 }

 private delegate void LabelVlaueSetter(string value);

 //Set method invoked by background thread
 private void SetLabelText(string value)
 {
   myLabel.Text = value;
 }






回答2:


As Johnathan Allen mentions, it should not matter. Unless something else is going on. I have had two cases where I could not interact with certain controls in the events generated by the BackgroundWorker. The only thing that worked was using the Invoke method.

Try assigning the DataSource on the same thread that created the DataGridView control. You do this through the control's Invoke method. Use the following code. (I have not tested, but this is the standard pattern.)

If this does not work then try Jonathan Allen's suggestion.

Actually, do whichever suggestion is easiest to try first.


    private delegate void SetDataSourceDelegate(object value);

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
        DataTable oData = null; //'assign data source
        if (dataGridView1.InvokeRequired) {
            dataGridView1.Invoke(new SetDataSourceDelegate(SetDataSource), new Object[] {oData});
        }else{
            SetDataSource(oData); 
        }
    }

    private void SetDataSource(object value) {
        dataGridView1.DataSource = value;
    }



回答3:


It shouldn't matter, but why are you using ProgressChanged instead of RunWorkerCompleted?

Also, try doing everything on the GUI thread without the BackgroundWorker. That will let you know if the problem is in your code or how your code interacts with the GUI.



来源:https://stackoverflow.com/questions/2505508/updating-datagrid-from-a-backgroundworker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!