从BackGroundWorker更新DataGrid [英] Updating DataGrid From a BackGroundWorker

查看:79
本文介绍了从BackGroundWorker更新DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的c#应用程序中有一个背景工作者和一个DataGrid.在做我的Backgroundworker的工作时,它将在我的dll中调用一个Api,这将在SQLite数据库中输入一些数据.在完成Api调用后,我报告了一个进度,在我的Backgroundworker正在进行事件中,我从Db获取了内容并将其作为数据源分配给我的网格.我在同一backgroundworker中调用相同的API.在处理过程中,我的应用程序崩溃了.但是,如果我没有在ProgressChanged中分配数据源,我的应用程序将不会崩溃.

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.

推荐答案

我假设您必须使用Invoke方法访问UI对象.

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;
 }



这篇关于从BackGroundWorker更新DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆