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

本文关键字:应用程序 挂起 工作程序 后台 | 更新日期: 2023-09-27 18:10:37

好的,这里是漂亮的代码:

// 
// 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里,也没有成功的东西

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

你在做一些不安全的事情:你在一个非UI线程上访问UI。您没有显式地这样做,但是您的UI绑定到您的表适配器。

你可能想要取消绑定,然后运行后台worker,然后(在完成事件时)重新绑定UI。

我很惊讶,当你在调试器中运行它时,它没有抛出异常,说实话…