如何关闭WPF中的所有打开表单

时间:2012-04-02 12:44:53

标签: wpf

在我的应用程序中,我有两个按钮“打开”和“关闭”。

当我点击打开按钮窗口时会打开,当我点击关闭按钮窗口时将会关闭。

当我单击打开按钮3次时,将打开3个窗口。我想点击关闭按钮时关闭所有窗口。

这是我的代码[请不要尝试更改线程,因为这是我在我的应用程序中的要求]

public partial class MainWindow : Window
{
    Window ProgressWindow;
    Thread ProgressThread;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Open_Click(object sender, RoutedEventArgs e)
    {
        ProgressThread = new Thread(() =>
        {
            ProgressWindow = new Window();
            ProgressWindow.Margin = new Thickness(0, 0, 50, 0);
            ProgressWindow.WindowState = WindowState.Normal;
            ProgressWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            ProgressWindow.Height = 180;
            ProgressWindow.Width = 180;
            ProgressWindow.Content = "Hello WPF";
            ProgressWindow.ShowInTaskbar = false;
            ProgressWindow.Show();
            ProgressWindow.Closed += (sender2, e2) =>
            ProgressWindow.Dispatcher.InvokeShutdown();
            System.Windows.Threading.Dispatcher.Run();
        });
        ProgressThread.SetApartmentState(ApartmentState.STA);
        ProgressThread.Start();
    }

    private void Close_Click(object sender, RoutedEventArgs e)
    {
        if (ProgressThread.IsAlive == true)
        {
            ProgressThread.Abort();
        } 
    }
}

3 个答案:

答案 0 :(得分:1)

我建议存储对已创建窗口的引用,您的代码可能如下所示:

Stack<Window> ProgressWindow=new Stack<Window>();
Thread ProgressThread;

private void Open_Click(object sender, RoutedEventArgs e)
{
    ProgressThread = new Thread(() =>
    {
        var window = new Window();
        window.Margin = new Thickness(0, 0, 50, 0);
        window.WindowState = WindowState.Normal;
        window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        window.Height = 180;
        window.Width = 180;
        window.Content = "Hello WPF";
        window.ShowInTaskbar = false;
        window.Show();
        window.Closed += (sender2, e2) =>
        window.Dispatcher.InvokeShutdown();
        ProgressWindow.Push(window);
        System.Windows.Threading.Dispatcher.Run();
    });
    ProgressThread.SetApartmentState(ApartmentState.STA);
    ProgressThread.Start();
}

private void Close_Click(object sender, RoutedEventArgs e)
{
    while (ProgressWindow.Count > 0)
    {
        ProgressWindow.Pop().Close();
    }
}
如果是应用程序的“正常”工作流程,则不推荐线程中止,即由于某些严重错误而未关闭窗口

答案 1 :(得分:0)

我不推荐你在做什么,实际上我真的不知道它是否像这样工作,但既然你说使用这样的线程是你的(奇怪的)要求,我只会评论实际问题:

您应该将线程保存在List中,然后关闭此列表中的所有线程。

修改

public partial class MainWindow : Window 
{ 
    Window ProgressWindow; 
    List<Thread> ProgressThreads = new List<Thread>();
    public MainWindow() 
    { 
        InitializeComponent(); 
    } 

    private void Open_Click(object sender, RoutedEventArgs e) 
    { 
        ProgressThreads.Add(new Thread(() => 
        { 
            ProgressWindow = new Window(); 
            ProgressWindow.Margin = new Thickness(0, 0, 50, 0); 
            ProgressWindow.WindowState = WindowState.Normal; 
            ProgressWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen; 
            ProgressWindow.Height = 180; 
            ProgressWindow.Width = 180; 
            ProgressWindow.Content = "Hello WPF"; 
            ProgressWindow.ShowInTaskbar = false; 
            ProgressWindow.Show(); 
            ProgressWindow.Closed += (sender2, e2) => 
            ProgressWindow.Dispatcher.InvokeShutdown(); 
            System.Windows.Threading.Dispatcher.Run(); 
        })); 
        ProgressThread.SetApartmentState(ApartmentState.STA); 
        ProgressThread.Start(); 
    } 

    private void Close_Click(object sender, RoutedEventArgs e) 
    { 
        foreach(var ProgressThread in ProgressThreads)
        {
            if (ProgressThread.IsAlive == true) 
            { 
                ProgressThread.Abort(); 
            }
        }  
    } 
} 

答案 2 :(得分:0)

单击“打开”时,您需要记录已打开的所有主题。然后在你的“关闭”方法循环遍历该列表,关闭每一个。

会员变量:

List<Thread> allThreads = new List<Thread>();

然后在你的开放处理程序中添加:

allThreads.Add(ProgressThread);

然后你的关闭处理程序变为:

foreach (Thread thread in allThreads)
{
    if (thread.IsAlive)
    {
        thread.Abort();
    }
}

你正在尝试的非正统应该不言而喻。