Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing all Windows in a C# WPF application

Tags:

c#

.net

wpf

I'm creating a little WPF app in VS2013Express and I've come across a little problem. You see, there are three windows, MainWindow, LatAndLongDialog, TimeCitiesDialog.

`LatAndLongDialog` and `TimeCitiesDialog` are opened from MainWindow (with the click of a button). I want all the other windows to close when the `Closed()` event is called on `MainWindow`. The code on MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GlobalTime_ELITE_for_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            UserDescText.Content =
            "Select a TimeCity or enter the latitude and longitude in \n" +
            "to view the World Time there. Or, select another one of the\n" +
            "options below to do that. Go to Help by clicking on the link\n" +
            "on the upper-right corner of the window to view everything you\n" +
            "can do.";
            this.Closed += CloseOff;
        }
        private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
        {
            TimeCitiesDialog ObjectReference = new TimeCitiesDialog();
            ObjectReference.Show();
        }
        private void OpenLatAndLongDialog(Object Sender, EventArgs E)
        {
            LatAndLongDialog ObjectReference = new LatAndLongDialog();
            ObjectReference.Show();
        }
        private void CloseOff(Object Sender, EventArgs E)
        {
            this.Close();
            TimeCitiesDialog tcdor = new TimeCitiesDialog();
            LatAndLongDialog laldor = new LatAndLongDialog();
        }
    }
}

How can I close them all? Please help!

like image 696
Aravind Suresh Avatar asked May 06 '16 03:05

Aravind Suresh


People also ask

Does closing doors help AC?

It can be tempting to slam the doors and hope the cool air builds up in the areas where you want it, but the fact is that modern air conditioning units are designed as whole-house systems. So when you shut that door, you're only making it much harder for your system to cool your home.

Should you close all windows on a hot day?

Should you keep your windows shut during hot weather? Generally speaking, you should open your windows if it's cooler outside than in. There's no 'set rule' for keeping windows open or closed during a bout of hot weather.

Does closing windows make it cooler?

Outdoor Temperature Opening the windows simply lets cool air escape and hot air to enter, resulting in hotter interiors. Before deciding to open all your windows at home during hot weather, grab a thermometer, and check if it's hotter outside the house. If it's cooler indoors, just close your windows.

Does cracking a window help air conditioning?

Even opening a window a crack is going to interfere with your air conditioner's efficiency. This creates vacuum pressure that will force the cool air out of your window and siphon air from other gaps in your home.


1 Answers

The proper way to shutdown a WPF app is to use Application.Current.Shutdown() . This will close all open Windows, raise some events so that cleanup code can be run, and it can't be canceled. Environment.Exit() terminates the application immediately even if other threads are executing.

You should also consider setting the Owner on non-main Windows. The behavior will likely be more like what you would expect in regards to Z-order, minimizing, and maximizing. As an added bonus, the owned windows will automatically close when the owner Window closes.

like image 175
James Durda Avatar answered Sep 28 '22 07:09

James Durda