CancelAsync 是否有效?

发布于 2024-11-02 05:17:40 字数 1274 浏览 0 评论 0原文

我制作了一个小型应用程序,其中 Form 是线程化的(使用 BackgroundWorker),并在表单中调用 QuitApplication 中的函数 QuitApplication当我想退出时,使用 code>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

I've made a small app where Form is threaded (using BackgroundWorker), and in the form I'm calling a function QuitApplication in Program class when I want to quit.

The DoWork looks like this:

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);
    }
}

and in the Form1 class i have this method attached to the closing of the window:

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

    Program.QuitApplication();
}

So what i want is to ensure that everything quits when I press the X on the window. However, the if( worker.CancellationPending == true ) never hits... why is this?

QuitApplication looks like this:

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

    Application.Exit();
}

And Im using guiThread.WorkerSupportsCancellation = true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2024-11-09 05:17:40

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;
    }
}

CancelAsync is setting the CancellationPending property, but then you immediately quit the application without giving the background thread a chance to detect that and shut down. You need to change your UI code to wait for the background thread to finish.

Personally, when I write apps like this, I make the form close button act like a Cancel button rather than quit immediately. It's a lot safer for the end user. For example:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文