问题
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