如何使用c#在.net中后台运行进程
我必须使用线程和后台工作人员在后台运行进程。这个过程执行从数据库检索数据的任务,它检索成功,因为我无法将该数据显示到datagridview中,并且出现一些数据错误事件。
I have to run process in background using thread and background worker. this process do the task of retrieve data from database,it retrieve successfully by I can not display that data into datagridview, there is give some data error event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BackgroundWorker 有一个内置的
RunWorkerCompleted
回调方法,您可以将其连接到该方法中,并自动处理 UI 线程的跨线程封送处理。在调用 RunWorkerAsync 之前,您可以连接到 RunWorkerCompleted 事件。在
backgroundWorker1_RunWorkerCompleted
中,您可以绑定网格或将 DataSource 属性设置为 DataTable,它将位于 UI 线程上。如果您已经完成此操作并收到错误,则可能只是 DoWork/线程代码本身中未处理的异常。如果是这种情况,请将 RunWorkerAsync 调用包装在 try catch 中,看看会得到什么。
希望有帮助...
BackgroundWorker has a built in callback method of
RunWorkerCompleted
that you wire into and it handles the cross thread marshaling to the UI thread automatically. Before you callRunWorkerAsync
you can wire into the theRunWorkerCompleted
event.within the
backgroundWorker1_RunWorkerCompleted
you can bind your grid or set the DataSource property to the DataTable and it will be on the UI thread.If you have already done this and are getting errors it might just be an unhandled exception within the DoWork/threaded code itself. If that is the case wrap the
RunWorkerAsync
call in a try catch and see what you get.Hope that help...
可能您无法修改 DataGridView 控件。
放
CheckForCrossThreadcalls = false
,它可能有效
May be you are unable to modify the DataGridView Control.
Set
CheckForCrossThreadcalls = false
,it may works