如何使用c#在.net中后台运行进程

发布于 2024-08-20 09:00:16 字数 92 浏览 2 评论 0原文

我必须使用线程和后台工作人员在后台运行进程。这个过程执行从数据库检索数据的任务,它检索成功,因为我无法将该数据显示到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-08-27 09:00:16

BackgroundWorker 有一个内置的 RunWorkerCompleted 回调方法,您可以将其连接到该方法中,并自动处理 UI 线程的跨线程封送处理。在调用 RunWorkerAsync 之前,您可以连接到 RunWorkerCompleted 事件。

backgroundWorker1.RunWorkerCompleted += 
                new RunWorkerCompletedEventHandler(
            backgroundWorker1_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 call RunWorkerAsync you can wire into the the RunWorkerCompleted event.

backgroundWorker1.RunWorkerCompleted += 
                new RunWorkerCompletedEventHandler(
            backgroundWorker1_RunWorkerCompleted);

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...

夜夜流光相皎洁 2024-08-27 09:00:16

可能您无法修改 DataGridView 控件。

CheckForCrossThreadcalls = false
,它可能有效

May be you are unable to modify the DataGridView Control.
Set
CheckForCrossThreadcalls = false
,it may works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文