您可以使用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