4

I have searched for creating a Singleton object for a window in WPF.

public static Test DefInstance
{
    get
    {
        if (formDefInstance == null)  // formDefInstance.IsDisposed
        {
            initializingDefInstance = true;
            formDefInstance = new cas18();
            initializingDefInstance = false;
        }
        return formDefInstance;
    }
    set { formDefInstance = value; }
}

But the forDefInstance.IsDisposed is not working and throwing an error.

Any Idea regarding this?

John Willemse
  • 6,608
  • 7
  • 31
  • 45
Hemant Kumar
  • 4,593
  • 9
  • 56
  • 95
  • 3
    There's no reason to downvote this question. From the code sample it is pretty clear what he wants to do: He wants the window to be opened only once, but also wants to detect if the window was closed. Then it should be possible to open it again, but never should there be two of these windows on screen at the same time. I actually think it's a pretty interesting question! – Thorsten Dittmar Apr 17 '13 at 09:20
  • 3
    @ThorstenDittmar: I was not the one who downvoted, but writing "... is not working and throwing an error" *without* including the error message is like asking to be downvoted. – Heinzi Apr 17 '13 at 09:22
  • 1
    @Heinzi While this is true that the question is not complete, I don't think it deserves a downvote. @ OP please add the error message. – ken2k Apr 17 '13 at 09:27
  • There is no `IsDisposed` property in the Window class (WPF). Are you talking about winforms instead? – ken2k Apr 17 '13 at 09:30
  • @ken2k Just a wild guess, but when the OP says *is not working and throwing an error*, could he just mean *I used IsDisposed in WinForms but now with WPF the compiler "throws an exception" saying there's no such method*? – Thorsten Dittmar Apr 17 '13 at 09:34
  • @ThorstenDittmar Agree, it sounds like the OP is trying to convert winforms to WPF and the "throwing an error" is at compile time. – ken2k Apr 17 '13 at 09:38
  • 1
    @ThorstenDittmar/ken2k: I suspect that's correct - and I'd say that it's a reasonable reason to downvote the question, at least in the absence of an edit or clarification from the OP. (I've not downvoted myself, but I can see why people might.) – Dan Puzey Apr 18 '13 at 14:24
  • http://stackoverflow.com/questions/1335785/how-can-i-make-sure-only-one-wpf-window-is-open-at-a-time and http://procbits.com/2010/12/29/forcing-single-instance-for-wpf – Francesco De Lisi Apr 18 '13 at 14:38
  • Since the question is still not completed and we still don't know what error the user ran into I must downvote this question. – Security Hound Apr 18 '13 at 15:51

3 Answers3

0

I don't know if it's what you want to do but it works for me :

private static MyWindow _defInstance;
public static MyWindow DefInstance
{
    get
    {
        if (null == _defInstance)
        {
            _defInstance = new MyWindow();
        }
        return _defInstance;
    }
 }

In MyWindow code :

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    this.Visibility = Visibility.Hidden;
    e.Cancel = true;
}

To use it :

DefInstance.Show();

Then, only one window is display and you use one instance of your window.

Floc
  • 668
  • 8
  • 16
0

I think everyone should take a look at Jon Skeet's C# In Depth site. If only to read and permanently burn into their brains the singleton patter a-la C#.

http://csharpindepth.com/Articles/General/Singleton.aspx

In your scenario, try to implement this (thread safe, non-lazy):

public sealed class DefInstance
{
  private static readonly DefInstance instance = new DefInstance();
  static DefInstance()
  {
  }

  private DefInstance()
  {
  }

  public static DefInstance Instance
  {
    get
    {
      return instance;
    }
   }
} 

There are also Lazy<T> implementions and various other implementations of the pattern in that site.

code4life
  • 15,655
  • 7
  • 50
  • 82
-2

you can achieve this by implementing following method

private static volatile DefInstance instance;
private static object syncRoot = new Object();

private DefInstance() {}

public static DefInstance Instance
{
   get 
   {
      if (instance == null) 
      {
         lock (syncRoot) 
         {
            if (instance == null) 
               instance = new DefInstance();
         }
      }

      return instance;
   }
}
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23
  • Whoa! Why volatile? The objective of a singleton is to create once and prevent subsequent instance creation. The `volatile` keyword makes it possible to create one or more subsequent instance and assign it to the `DefInstance`. Use `readonly` instead. – code4life Apr 18 '13 at 14:30
  • the variable is declared to be volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed – Shafqat Masood Apr 18 '13 at 14:32
  • 1
    `private static readonly DefInstance instance = new DefInstance()` ensures thread safety and accessibility, and prevents subsequent writes. `volatile` does not. – code4life Apr 18 '13 at 14:37
  • please read this question answer then you will be able to know why i have mention to use volatile.. http://stackoverflow.com/questions/1964731/the-need-for-volatile-modifier-in-double-checked-locking-in-net – Shafqat Masood Apr 18 '13 at 16:32
  • and i have mentioned double checked locking method in the example.. for more on to this concept you can refer to this document http://en.wikipedia.org/wiki/Double-checked_locking – Shafqat Masood Apr 18 '13 at 16:36
  • The wikipedia example cites exactly what I'm talking about: `private static readonly`. BTW the -1 is not from me... I'm just commenting, that's all. – code4life Apr 18 '13 at 18:48