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

时间:2022-08-28 07:38:48

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在“进度”窗口中定义:

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.

1 个解决方案

#1


7  

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

#1


7  

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