后台工作者挂起整个应用程序

发布于 2024-12-08 20:28:48 字数 1228 浏览 2 评论 0原文

好的,这是非常简单的代码:

// 
// 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 中锁定工作线程类,但没有成功。还尝试在 DoWork 中锁定 this.DSNumConfigs ,但也没有任何成功的东西。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

甜中书 2024-12-15 20:28:48

好吧,您正在做一些不安全的事情:您正在非 UI 线程上访问 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.

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

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