您可以使用e.Result这样的: 第一步:你需要添加RunWorkerCompleted事件。
_bw.RunWorkerCompleted += BwRunWorkerCompleted;
,并创建功能BwRunWorkerCompleted,线程完成后,这将被解雇。例如:
private void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
//
if (e.Result != null)
{
// test if result is true or false <== HERE YOU GO!
if ((Boolean)e.Result == true)
{
this.Hide();
Form fm = new SFGrilla(ref lr, ref binding);
fm.ShowDialog();
this.Close();
}
}
else
{
// hide the progress bar when the long running process finishes
progressBar.Visible = false;
// enable button
btnLogin.Enabled = true;
}
}
你testbool()应该有合适的参数:
private void testbool(object sender, DoWorkEventArgs doWorkEventArgs) {
success = true;
doWorkEventArgs.Result = success;
}
希望它能帮助。
不,这是不可能的。使用BackgroundWorker的要点是在另一个线程上运行代码。这需要花时间来计算结果。等待这个结果完全违背了使用worker的点,并且很可能导致死锁,你也可以直接调用testBool()。 – 2013-04-22 13:55:47