|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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/ |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
That will work fine, although you will have to remember to manually dispose the form once your application doesn't need it anymore.
|
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
Quote:
In your GetInstance method: Code:
this.instance.FormClosed += (sender,e) => this.instance = null; |
|
#7
|
|||
|
|||
|
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? |
|
#8
|
|||
|
|||
|
Quote:
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;
}
|
|
#9
|
|||
|
|||
|
Quote:
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. |
|
#10
|
|||
|
|||
|
ah okay what I did was just overrode the dispose method and in it I just said instance == null. Thanks everyone!
|
|
#11
|
||||
|
||||
|
Quote:
You're not disposing of resources, so you don't override the dispose method. Override OnFormClosed instead. As noted in the documentation:Quote:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > C# Singleton problems... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|