Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. singelton problem

singelton problem

Scheduled Pinned Locked Moved C#
questionregexhelp
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akkram
    wrote on last edited by
    #1

    Hi, im using a singelton pattern to ensure that only one innstance of a winfrom can be opened at one time. The problem im having is when I close the form and want to open it again I get a "Cannot access a disposed object." How can I solve this? //thx

    L M 2 Replies Last reply
    0
    • A akkram

      Hi, im using a singelton pattern to ensure that only one innstance of a winfrom can be opened at one time. The problem im having is when I close the form and want to open it again I get a "Cannot access a disposed object." How can I solve this? //thx

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, if you want to see the same instance of your form class again, you should hide it instead of closing it; so use Form.Hide() or Form.Visible=false :)

      Luc Pattyn [My Articles] [Forum Guidelines]

      A 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, if you want to see the same instance of your form class again, you should hide it instead of closing it; so use Form.Hide() or Form.Visible=false :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        A Offline
        A Offline
        akkram
        wrote on last edited by
        #3

        Thx, but I don't want to show the same instance of the form. I wan't to be able to create a new instance but only have one open at the same time.

        L G 2 Replies Last reply
        0
        • A akkram

          Thx, but I don't want to show the same instance of the form. I wan't to be able to create a new instance but only have one open at the same time.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          I think you need to show some code, with form creation, Dispose, your current' singleton stuff, etc. And what should (not) cause the creation of a new form ? :)

          Luc Pattyn [My Articles] [Forum Guidelines]

          A 1 Reply Last reply
          0
          • A akkram

            Thx, but I don't want to show the same instance of the form. I wan't to be able to create a new instance but only have one open at the same time.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            The singleton pattern is not for using one object at a time, it's for reusing the same object. If you don't want to reuse the form instance, you should not use the singleton pattern.

            --- single minded; short sighted; long gone;

            1 Reply Last reply
            0
            • L Luc Pattyn

              I think you need to show some code, with form creation, Dispose, your current' singleton stuff, etc. And what should (not) cause the creation of a new form ? :)

              Luc Pattyn [My Articles] [Forum Guidelines]

              A Offline
              A Offline
              akkram
              wrote on last edited by
              #6

              So some samplecode then. when the user clicks the button the new form opens. He should not be able to open any more forms of this type unless he closes the form. inside main form: private void button1_Click(object sender, EventArgs e) { Form2 form = Form2.Instance(); form.Show(); } inside form2: private static Form2 instance; public static Form2 Instance() { if (instance == null) { instance = new Form2(); } return instance; } protected Form2() { }

              L 1 Reply Last reply
              0
              • A akkram

                So some samplecode then. when the user clicks the button the new form opens. He should not be able to open any more forms of this type unless he closes the form. inside main form: private void button1_Click(object sender, EventArgs e) { Form2 form = Form2.Instance(); form.Show(); } inside form2: private static Form2 instance; public static Form2 Instance() { if (instance == null) { instance = new Form2(); } return instance; } protected Form2() { }

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, from your code it is apparant you implemented a singleton: initially there is no instance of Form2; once you call Form2.Instance, there is exactly one. If you call Form2.Instance again, you get the same instance. This is not what you want for two reasons: - if you close the "first" Form2, it renders all future Form2 instances useless since you can close a Form only once - it does not prevent you from calling Form.Show() twice. What you need instead is no singleton logic; just create a Form2 when you need one; use it; get rid of it with Close(). And add some logic to prevent two Form2 instances to coexist; possible solutions are: - disable the button while a Form2 is visible (best, user can see it!) - or include a test in buttonClick, something like:

                private Form2 lastForm2; // class member
                
                if (lastForm2==null || !lastForm2.Visible) {
                    lastForm2=new Form2();
                    lastForm2.Show();
                }
                

                :)

                Luc Pattyn [My Articles] [Forum Guidelines]

                1 Reply Last reply
                0
                • A akkram

                  Hi, im using a singelton pattern to ensure that only one innstance of a winfrom can be opened at one time. The problem im having is when I close the form and want to open it again I get a "Cannot access a disposed object." How can I solve this? //thx

                  M Offline
                  M Offline
                  Muammar
                  wrote on last edited by
                  #8

                  Form1 form1;
                  ...
                  ...
                  ...

                  if(form1==null)
                  Form1 form1 = new Form1();
                  form1.Show();

                  And this's all it takes!


                  Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                  L A 2 Replies Last reply
                  0
                  • M Muammar

                    Form1 form1;
                    ...
                    ...
                    ...

                    if(form1==null)
                    Form1 form1 = new Form1();
                    form1.Show();

                    And this's all it takes!


                    Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi Muammar, almost; you should drop one "Form1" !

                    Luc Pattyn [My Articles] [Forum Guidelines]

                    1 Reply Last reply
                    0
                    • M Muammar

                      Form1 form1;
                      ...
                      ...
                      ...

                      if(form1==null)
                      Form1 form1 = new Form1();
                      form1.Show();

                      And this's all it takes!


                      Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

                      A Offline
                      A Offline
                      akkram
                      wrote on last edited by
                      #10

                      thx, searched for a while and finally found why I didn't get it to work. Apparently I also need to check if the form has been disposed of. private Form2 form2; .. .. private void button1_Click(object sender, EventArgs e) { if (form2 == null || form2.IsDisposed) { form2 = new Form2(); form2.Show(); } }

                      A 1 Reply Last reply
                      0
                      • A akkram

                        thx, searched for a while and finally found why I didn't get it to work. Apparently I also need to check if the form has been disposed of. private Form2 form2; .. .. private void button1_Click(object sender, EventArgs e) { if (form2 == null || form2.IsDisposed) { form2 = new Form2(); form2.Show(); } }

                        A Offline
                        A Offline
                        Alaric_
                        wrote on last edited by
                        #11

                        I believe checking form2.IsDisposed will create the functionality that you are looking for, but from reading this thread, it seems like you are inappropriately applying the Singleton pattern if you are needing to perform this check. You might want to read up on your Singleton & Other GoF patterns[^] The Singleton is not really intended for flow control between forms unless it is necessary that (for some reason) you require the EXACT SAME instance of the form throughout the entire application process.(Singletons are more appropriately applied(for example) to concurrent invocation of a state machine on a remote server....or for the Load Balancing example given at the link I included above) Unless you have a very good reason for doing this, then using a Singleton convolutes your logic, and should be avoided. If you are simply wanting to restrict your user to only maintaining one instance of the form at any one time, then you could simply disable the ability to create a separate instance as was demonstrated in previous replies

                        Allow user to instantiate a new form(You're probably wanting it to be Modal)
                        Restrict user from instantiating a second instance of the form
                        User invokes some business logic on the form
                        User closes form when finished

                        If the user wants to replicate the process(let's say...the form allows the user to perform some calculation based upon values selected in an UltraWebGrid), the process would be repeated.

                        Welcome my son...Welcome..to the Machine

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups