Window.Close()在WPF中是否同步?

时间:2013-06-12 18:30:38

标签: wpf asynchronous window

正如标题所述,我想知道窗口上的Close()方法是否同步。

我浏览了msdn,但无法找到答案。

提前感谢您的回复

1 个答案:

答案 0 :(得分:4)

Window.Close()在WPF中是同步的。

您可以通过向ClosingClosed事件添加事件处理程序来轻松测试,并跟踪执行情况:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{            
    Debug.WriteLine("Closing");
    this.Closing += OnClosing;
    this.Closed += OnClosed;
    this.Close();
    Debug.WriteLine("Closed");
}

private void OnClosed(object sender, EventArgs eventArgs)
{
    Debug.WriteLine("In OnClosed");
}

private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
{
    Debug.WriteLine("In OnClosing");
}

这将打印出来:

Closing
In OnClosing
In OnClosed
Closed