后台工作者挂起整个应用程序 [英] Background worker hangs whole application

查看:243
本文介绍了后台工作者挂起整个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是漂亮的代码:

Ok here's the pretty light code:

// 
// numConfigsBindingSource
// 
this.numConfigsBindingSource.DataMember = "NumConfigs";
this.numConfigsBindingSource.DataSource = this.DSNumConfigs;

// Grid
this.GridNumConfigs.DataSource = this.numConfigsBindingSource;

// 
// DSNumConfigs
// 
this.DSNumConfigs.DataSetName = "DSNumConfigs";
this.DSNumConfigs.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
// 
// numConfigsTableAdapter
// 
this.numConfigsTableAdapter.ClearBeforeFill = false;
//
// 
// DSConfigNumbers
// 
this.DSConfigNumbers.DataSetName = "DSConfigNumbers";
this.DSConfigNumbers.EnforceConstraints = false;
this.DSConfigNumbers.SchemaSerializationMode =       System.Data.SchemaSerializationMode.IncludeSchema;

private void Form1_Load(object sender, EventArgs e)
{                
    worker.RunWorkerAsync();
}

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    this.numConfigsTableAdapter.Fill(this.DSNumConfigs.NumConfigs);                    
}

然后我在VS2010下运行此代码,可以找到它,但是当我运行发布应用程序时,它挂了.但是,如果我重写此未使用BackgroundWorkers的代码,则效果很好. 我需要做出一些努力来明确释放后台工作人员吗?我试图将工人类锁定在Form1_Load中,但未提供任何成功.还尝试将this.DSNumConfigs锁定在DoWork中,也没有任何成功的东西.

Then I run this code under VS2010 it works find but when I just run release application it's hanging. But if I rewrite this code that isn't using BackgroundWorkers it's works fine. Do I need some effort to clearly release background worker? I have tried to lock worker class in Form1_Load it isn't provide any success. Also have tried to lock this.DSNumConfigs in DoWork and also no any successful stuff.

推荐答案

好,您正在做一些不安全的事情:您正在非UI线程上访问UI.您不是明确地这样做,但是您的UI绑定到了表适配器.

Well, you're doing something unsafe: you're accessing the UI on a non-UI thread. You're not doing so explicitly, but your UI is bound to your table adapter.

您可能要取消绑定,然后运行后台工作程序,然后(在完成的事件上)重新绑定UI.

You may want to unbind, then run the background worker, then (on the completed event) rebind the UI.

很奇怪,老实说,当您在调试器中运行它时,它没有引发异常.

I'm surprised that it's not throwing an exception when you run it in the debugger, to be honest...

这篇关于后台工作者挂起整个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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