CancelAsync是否正常工作?

时间:2011-04-18 12:35:16

标签: c# multithreading

我创建了一个小型应用,其中Form具有线程性(使用BackgroundWorker),并且我在QuitApplication类中调用函数Program的形式我想退出。

DoWork看起来像这样:

static void guiThread_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    while (true)
    {
        if (worker.CancellationPending == true)
        {
            e.Cancel = true;
            break;
        }

        if (Program.instance.form != null)
        {
            Program.instance.form.UpdateStatus(Program.instance.statusText, Program.instance.statusProgress);
        }

        Thread.Sleep(GUI_THREAD_UPDATE_TIME);
    }
}

并且在Form1类中,我将此方法附加到窗口的关闭位置:

void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Program.instance.SetStatus("Closing down...", 0);

    Program.QuitApplication();
}

所以我想要的是确保当我按下窗口上的X时一切都会退出。但是,if( worker.CancellationPending == true )从未命中......为什么会这样?

QuitApplication如下所示:

public static void QuitApplication()
{
    Program.instance.guiThread.CancelAsync();

    Application.Exit();
}

我正在使用guiThread.WorkerSupportsCancellation = true

1 个答案:

答案 0 :(得分:3)

CancelAsync正在设置CancellationPending属性,但是您立即退出应用程序,而不会让后台线程有机会检测到并关闭。您需要更改UI代码以等待后台线程完成。

就个人而言,当我编写这样的应用程序时,我将表单关闭按钮设置为取消按钮,而不是立即退出。这对最终用户来说更安全。例如:

private void abortButton_Click(object sender, EventArgs e) {
    // I would normally prompt the user here for safety.
    worker.CancelAsync();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    if(worker.IsBusy) {
        // If we are still processing, it's not very friendly to suddenly abort without warning.
        // Convert it into a polite request instead.
        abortButton.PerformClick();
        e.Cancel = true;
    }
}