使用 Dispatcher.Invoke 从 WPF 中的不同线程关闭窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11747469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:47:25  来源:igfitidea点击:

Close window from different thread in WPF using Dispatcher.Invoke

c#.netwpfmultithreading

提问by ChrisO

I have a problem that I cannot solve even after looking at the various information about Dispatcher.Invoke.

我有一个问题,即使查看了有关 Dispatcher.Invoke 的各种信息也无法解决。

There is a MainWindow that creates a task, starts that task and then opens a progress window. When the task completes, I need to close the progress window. Here's what I have:

有一个 MainWindow 可以创建一个任务,启动该任务,然后打开一个进度窗口。任务完成后,我需要关闭进度窗口。这是我所拥有的:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Task task = new Task(_vm.WriteToDB);
        task.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted);
        task.ContinueWith(RanToCompletion, TaskContinuationOptions.OnlyOnRanToCompletion);
        task.Start();

        _frmProgress = new Progress(task);
        _frmProgress.DataContext = _vm;
        _vm.ProgressText = "Connecting to update server...";
        _frmProgress.ShowDialog();
    }

    public void RanToCompletion(Task task)
    {
        Progress.FormCloseDelegate FormCloseDelegate = new Progress.FormCloseDelegate(_frmProgress.Close);

        if (_frmProgress.Dispatcher.CheckAccess())
            _frmProgress.Close();
        else
            _frmProgress.Dispatcher.Invoke(DispatcherPriority.Normal, FormCloseDelegate);
    }

FormCloseDelegate is defined in the Progress window:

FormCloseDelegate 在 Progress 窗口中定义:

public delegate void FormCloseDelegate();

With the current implementation above, the code compiles and runs but the Progress form is not closed once the task completes. Any ideas?

使用上面的当前实现,代码编译并运行,但任务完成后不会关闭 Progress 表单。有任何想法吗?

I'd also be open to other ideas to solve this. Maybe I've gone down the wrong path altogether.

我也愿意接受其他想法来解决这个问题。也许我完全走上了错误的道路。

Thank you.

谢谢你。

回答by ChrisO

Well as I mentioned in comments, I was blocking closing of the Progress window when the user clicked on the close button with e.Cancel=true in the Window_Closing event. This was blocking the call in the code to close the window also.

正如我在评论中提到的那样,当用户在 Window_Closing 事件中单击带有 e.Cancel=true 的关闭按钮时,我阻止了 Progress 窗口的关闭。这也阻止了代码中关闭窗口的调用。

I updated the RanToCompletion method after realising that I didn't need a custom delegate at all:

在意识到我根本不需要自定义委托后,我更新了 RanToCompletion 方法:

public void RanToCompletion(Task task)
    {
        if (_frmProgress.Dispatcher.CheckAccess())
            _frmProgress.Close();
        else
            _frmProgress.Dispatcher.Invoke(DispatcherPriority.Normal, new ThreadStart(_frmProgress.Close));
    }