Background worker hangs whole application

岁酱吖の 提交于 2019-12-25 02:12:26

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/7690767/background-worker-hangs-whole-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!