1

Hey I want to make sure that I can only open ONE instance of this window, it doesn't seem to be working and not sure why.

I'm checking there is already a window open with the same name, and making sure im not detecting this current window attempting to open.

public new void Show()
{
    //Ensure new notifications are placed above older ones
    foreach (Window window in System.Windows.Application.Current.Windows)
    {
        string windowName = window.GetType().Name;
        if (!windowName.Equals("NotificationAll") && window != this)
        {
            this.Topmost = true;
            base.Show();

            this.Owner = System.Windows.Application.Current.MainWindow;

            //Position the Notification
            var workingArea = SystemParameters.WorkArea;
            this.Left = (workingArea.Width - this.ActualWidth) / 2;
            this.Top = workingArea.Bottom - this.ActualHeight;
        }
    }            
}

However more than one window is opening still!

Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • 1
    Are you trying to make sure only 1 instance of your application itself is running? If so, [look here](http://stackoverflow.com/questions/6486195/ensuring-only-one-application-instance). – sab669 Nov 16 '15 at 20:52
  • What do you want to do with previously opened window(s)? Close them or close current one? – TheVillageIdiot Nov 16 '15 at 20:52
  • @sab669 Nah it's an additional Window – Martyn Ball Nov 16 '15 at 20:55
  • @TheVillageIdiot Just stop the current one from being opened, which is why I put it in the IF statement expecting it not to be shown if there is an instance already existing – Martyn Ball Nov 16 '15 at 20:56
  • 1
    In that case, a lot of people like to use the singleton pattern for things like this. Many would also argue *against* its use and call it an "antipattern", but it's up to you really. – sab669 Nov 16 '15 at 20:57
  • 1
    If you look at your code carefully, you'll see that what it's really doing is this: it'll show the new window for each window it finds that is not a `NotificationAll` window. Remember, that code is inside a `foreach` loop. Step through it with a debugger and you'll see what I mean. – Pieter Witvoet Nov 16 '15 at 21:13
  • 1
    Personally I'd make the constructors private and then have a static "Create" method that would either create a new instance if there is none, or activate the existing instance. The `Show` function seems like it is way too late in the process to enforce a single instance. You can use a static field to track if there is an instance or not, or `Lazy`. – Ron Beyer Nov 16 '15 at 21:20

2 Answers2

1

To check, if there is no other Window with the same name you could use this Linq statement:

if (!Application.Current.Windows.Cast<Window>().Where(x => 
    x != this).Any(x => x.GetType().Name == "NotificationAll"))
{

}
Flat Eric
  • 7,971
  • 9
  • 36
  • 45
1

You are not doing anything to previous windows opened. Try this modification:

public new void Show()
{
    //Ensure new notifications are placed above older ones
    foreach (Window window in System.Windows.Application.Current.Windows)
    {
        string windowName = window.GetType().Name;
        //ALSO CHECK BY PLACING BREAKPOINT AT THIS if TO SEE WHAT WINDOW
        //NAME ARE YOU GETTING OR IF YOU ARE ENTRING THIS BLOCK
        if (windowName.Equals("NotificationAll") && window != this)
        {
           //IF YOU WANT TO CLOSE PREVIOUS WINDOWS
           window.Close();
        }
     }

      //NOW MANIPLUATE CURRENT WINDOW'S PROPERTIES AND SHOW IT
      this.Topmost = true;
      base.Show();
       ....
       ....
       this.Top = workingArea.Bottom - this.ActualHeight;
}

If you want to close current window and show previous one:

public new void Show()
{
    var hasOtherWindow=false;
    //Ensure new notifications are placed above older ones
    foreach (Window window in System.Windows.Application.Current.Windows)
    {
        string windowName = window.GetType().Name;
        if (!windowName.Equals("NotificationAll") && window != this)
        {
            hasOtherWindow=true;
            window.Topmost = true;
            //Position the Notification
            var workingArea = SystemParameters.WorkArea;
            window.Left = (workingArea.Width - window.ActualWidth) / 2;
            window.Top = workingArea.Bottom - window.ActualHeight;
            break;//GET OUT OF LOOP YOU WILL HAVE ONLY ONE WINDOW
        }
    }
    if(hasOtherWindow)
    Close();//CLOSE THIS WINDOW
}
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188