C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 1st, 2009, 11:20 AM
Optimum Optimum is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2002
Posts: 526 Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 10 m 4 sec
Reputation Power: 31
Send a message via AIM to Optimum
C# Singleton problems...

I am trying to create singletons of form classes so that only one instance of the form can be opened. This one form that I made a singleton is launched from the main form as such:

Code:
        private void editUsersButton_Click(object sender, EventArgs e)
        {
            EditUsers editUsers = EditUsers.getInstance();
            editUsers.Show();
        }


This works fine until I close out the EditUsers window and then try and click this button again. I get an exception thrown saying that the resource has been unallocated or something... like deleted... How can I prevent this? I guess when I hit the "X" on the form it destroys the object which makes sense. But getInstance() still returns a reference to SOMETHING. What's going on here? Thanks

Reply With Quote
  #2  
Old July 1st, 2009, 11:47 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,379 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 45 m 18 sec
Reputation Power: 563
The singleton behavioral pattern for Windows is something like:

If not windowCreated CreateWindow
Get instance
Show instance
If window destroyed clear windowCreated

What is your EditUsers class derived from?
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/

Reply With Quote
  #3  
Old July 3rd, 2009, 05:48 AM
280Z28 280Z28 is offline
Turning coffee into code
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: UT Austin, TX
Posts: 164 280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 20 h 37 m
Reputation Power: 89
Send a message via AIM to 280Z28
You want to force the window to hide instead of close when the user clicks the X:

Override the OnClosing method in your EditUsers form as follows:

Code:
protected override void OnClosing(CancelEventArgs e)
{
    e.Cancel = true;
    Hide();
}


This may or may not work and/or be a complete answer. It's been a long time now.

Last edited by 280Z28 : July 3rd, 2009 at 05:51 AM.

Reply With Quote
  #4  
Old July 4th, 2009, 05:33 AM
scoper's Avatar
scoper scoper is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Surrey, UK
Posts: 961 scoper User rank is Captain (20000 - 30000 Reputation Level)scoper User rank is Captain (20000 - 30000 Reputation Level)scoper User rank is Captain (20000 - 30000 Reputation Level)scoper User rank is Captain (20000 - 30000 Reputation Level)scoper User rank is Captain (20000 - 30000 Reputation Level)scoper User rank is Captain (20000 - 30000 Reputation Level)scoper User rank is Captain (20000 - 30000 Reputation Level)scoper User rank is Captain (20000 - 30000 Reputation Level)scoper User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 1 h 15 m 20 sec
Reputation Power: 253
Send a message via MSN to scoper Send a message via Google Talk to scoper Send a message via Skype to scoper
That will work fine, although you will have to remember to manually dispose the form once your application doesn't need it anymore.
__________________
Scott Perham - MCPD

That URL too long? Why not URL IT!

Reply With Quote
  #5  
Old July 6th, 2009, 12:45 PM
Optimum Optimum is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2002
Posts: 526 Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 10 m 4 sec
Reputation Power: 31
Send a message via AIM to Optimum
I don't want the form to hide when the X is clicked. I want the object to be destroyed when the X is clicked, so that when I try to make another singleton instance of the class there are no problems. Thanks for responses tho

Reply With Quote
  #6  
Old July 6th, 2009, 12:53 PM
280Z28 280Z28 is offline
Turning coffee into code
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: UT Austin, TX
Posts: 164 280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 20 h 37 m
Reputation Power: 89
Send a message via AIM to 280Z28
Quote:
Originally Posted by Optimum
I don't want the form to hide when the X is clicked. I want the object to be destroyed when the X is clicked, so that when I try to make another singleton instance of the class there are no problems. Thanks for responses tho


In your GetInstance method:

Code:
this.instance.FormClosed += (sender,e) => this.instance = null;

Reply With Quote
  #7  
Old July 6th, 2009, 01:32 PM
Optimum Optimum is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2002
Posts: 526 Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 10 m 4 sec
Reputation Power: 31
Send a message via AIM to Optimum
Could you explain what that is doing?

Here is my current get instance code:

Code:
        public static EditUsers getInstance(Dictionary<string, Dictionary<string, string>> localAccounts, Dictionary<string, Dictionary<string, string>> networkAccounts, List<string> localGroups, List<string> networkGroups)
        {
            this.instance.FormClosed += (sender, e) => this.instance = null;

            if (instance == null)
                instance = new EditUsers(localAccounts, networkAccounts, localGroups, networkGroups);
            return instance;
        }


that's where I should put it?

Do I need any of the other code from the other posters?

Reply With Quote
  #8  
Old July 6th, 2009, 07:15 PM
280Z28 280Z28 is offline
Turning coffee into code
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: UT Austin, TX
Posts: 164 280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 20 h 37 m
Reputation Power: 89
Send a message via AIM to 280Z28
Quote:
Originally Posted by Optimum
Could you explain what that is doing?

Here is my current get instance code:

Code:
        public static EditUsers getInstance(Dictionary<string, Dictionary<string, string>> localAccounts, Dictionary<string, Dictionary<string, string>> networkAccounts, List<string> localGroups, List<string> networkGroups)
        {
            this.instance.FormClosed += (sender, e) => this.instance = null;

            if (instance == null)
                instance = new EditUsers(localAccounts, networkAccounts, localGroups, networkGroups);
            return instance;
        }


that's where I should put it?

Do I need any of the other code from the other posters?


It says: "If the form is open, make sure there is only a single instance. When the form is closed, I no longer want to use this instance (since it's closed), so set the singleton to null, and the next time the form is requested it'll create a new one."

Here's how it should look done that way:

Code:
public static EditUsers GetInstance(...)
{
  if (instance == null)
  {
    instance = new EditUsers(...);
    instance.FormClosed += (sender, e) => instance = null;
  }

  return instance;
}


However, I highly recommend you use this pattern instead, so you can distiguish between "If the form is open, give me the pointer to it," and "Create the form only if it doesn't exist, then return a pointer to the open form."

Code:
public static EditUsers Instance
{
  get { return instance; }
  private set { instance = value; }
}

// There are better ways to make this thread safe.
[MethodImpl(MethodImplOptions.Synchronized)]
public static EditUsers GetInstance(...)
{
  if (Instance == null)
  {
    Instance = new EditUsers(...);
    Instance.FormClosed += (sender,e) => Instance = null;
  }

  return Instance;
}

Reply With Quote
  #9  
Old July 6th, 2009, 07:20 PM
280Z28 280Z28 is offline
Turning coffee into code
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: UT Austin, TX
Posts: 164 280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 20 h 37 m
Reputation Power: 89
Send a message via AIM to 280Z28
Quote:
Originally Posted by scoper
That will work fine, although you will have to remember to manually dispose the form once your application doesn't need it anymore.


The form is automatically disposed when it's closed.

Edit: I see you were commenting on hiding the form. You are absolutely correct, there would need to be another method to close/dispose the form in that case.

Reply With Quote
  #10  
Old July 6th, 2009, 08:03 PM
Optimum Optimum is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2002
Posts: 526 Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level)Optimum User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 10 m 4 sec
Reputation Power: 31
Send a message via AIM to Optimum
ah okay what I did was just overrode the dispose method and in it I just said instance == null. Thanks everyone!

Reply With Quote
  #11  
Old July 6th, 2009, 09:44 PM
280Z28 280Z28 is offline
Turning coffee into code
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: UT Austin, TX
Posts: 164 280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 20 h 37 m
Reputation Power: 89
Send a message via AIM to 280Z28
Quote:
Originally Posted by Optimum
ah okay what I did was just overrode the dispose method and in it I just said instance == null. Thanks everyone!


You're not disposing of resources, so you don't override the dispose method. Override OnFormClosed instead. As noted in the documentation:

Quote:
The OnFormClosed method also allows derived classes to handle the event without attaching a delegate. Overriding this method is the preferred technique for handling the event in a derived class.

Notes to Inheritors:

When overriding OnFormClosed in a derived class, be sure to call the base class's OnFormClosed method so that registered delegates receive the event.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C# Singleton problems...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT