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. Closing and recreating a form

Closing and recreating a form

Scheduled Pinned Locked Moved C#
questionperformance
28 Posts 6 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.
  • S Offline
    S Offline
    SimpleData
    wrote on last edited by
    #1

    Hi I have a Windows form called mainwindow in my project. After it completes what it does, it closes and then recreates itself. But as I have noticed lately it recreates but doesnt't close itself. Here is my code:

    private void RestartAll()
    {
    // These are not that important, only to transfer the same properties of the form to the new form.
    depo.multipleformlocation=this.Location;
    depo.multipleform = true;
    depo.ekrangoruntusuadedi = 0;

    this.Hide();

    mainwindow _mainwindow = new mainwindow();
    _mainwindow.ShowDialog();

    this.Close();

    // Here is my temporary solution (putting old form's thread to sleep infinitely because it shouldn't keep doing what it does)
    Thread.Sleep(Timeout.Infinite);
    }

    How can I close(destroy) the old form completely and recreate that form? When I don't completely close that old form, because I do this closing and recreating process more than once my App's memory usage gets much higher. Thanks in advance.

    H L L 3 Replies Last reply
    0
    • S SimpleData

      Hi I have a Windows form called mainwindow in my project. After it completes what it does, it closes and then recreates itself. But as I have noticed lately it recreates but doesnt't close itself. Here is my code:

      private void RestartAll()
      {
      // These are not that important, only to transfer the same properties of the form to the new form.
      depo.multipleformlocation=this.Location;
      depo.multipleform = true;
      depo.ekrangoruntusuadedi = 0;

      this.Hide();

      mainwindow _mainwindow = new mainwindow();
      _mainwindow.ShowDialog();

      this.Close();

      // Here is my temporary solution (putting old form's thread to sleep infinitely because it shouldn't keep doing what it does)
      Thread.Sleep(Timeout.Infinite);
      }

      How can I close(destroy) the old form completely and recreate that form? When I don't completely close that old form, because I do this closing and recreating process more than once my App's memory usage gets much higher. Thanks in advance.

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      I am having a little difficulty in understanding where the cause of your problem might lie. Just to clarify things, what class contains the RestartAll method in the code you posted?

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      S 1 Reply Last reply
      0
      • H Henry Minute

        I am having a little difficulty in understanding where the cause of your problem might lie. Just to clarify things, what class contains the RestartAll method in the code you posted?

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        S Offline
        S Offline
        SimpleData
        wrote on last edited by
        #3

        RestartAll method is in the mainform class.

        H 1 Reply Last reply
        0
        • S SimpleData

          RestartAll method is in the mainform class.

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Try this:

          private void RestartAll()
          {
          // These are not that important, only to transfer the same properties of the form to the new form.
          depo.multipleformlocation=this.Location;
          depo.multipleform = true;
          depo.ekrangoruntusuadedi = 0;

          this.Hide();

          using (mainwindow _mainwindow = new mainwindow())
          {
          _mainwindow.ShowDialog();
          }

          this.Close();

          // Here is my temporary solution (putting old form's thread to sleep infinitely because it shouldn't keep doing what it does)
          Thread.Sleep(Timeout.Infinite);
          }

          Using the using construct 'should' ensure that _mainwindow is disposed.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          S 1 Reply Last reply
          0
          • H Henry Minute

            Try this:

            private void RestartAll()
            {
            // These are not that important, only to transfer the same properties of the form to the new form.
            depo.multipleformlocation=this.Location;
            depo.multipleform = true;
            depo.ekrangoruntusuadedi = 0;

            this.Hide();

            using (mainwindow _mainwindow = new mainwindow())
            {
            _mainwindow.ShowDialog();
            }

            this.Close();

            // Here is my temporary solution (putting old form's thread to sleep infinitely because it shouldn't keep doing what it does)
            Thread.Sleep(Timeout.Infinite);
            }

            Using the using construct 'should' ensure that _mainwindow is disposed.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            S Offline
            S Offline
            SimpleData
            wrote on last edited by
            #5

            Thank you. It seems to be working. Should I keep using the Thread.Sleep(Timeout.Infinite); line?

            H 1 Reply Last reply
            0
            • S SimpleData

              Thank you. It seems to be working. Should I keep using the Thread.Sleep(Timeout.Infinite); line?

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              Try commenting it out and see if everything still works. As you said in your OP it was a temporary solution, and shouldn't be necessary. However, if you still have the old problem after commenting it, the fix hasn't worked and it's back to the drawing-board.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              S 1 Reply Last reply
              0
              • H Henry Minute

                Try commenting it out and see if everything still works. As you said in your OP it was a temporary solution, and shouldn't be necessary. However, if you still have the old problem after commenting it, the fix hasn't worked and it's back to the drawing-board.

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                S Offline
                S Offline
                SimpleData
                wrote on last edited by
                #7

                I have commented it and seems like it's still working. Thank you for your help.

                H 1 Reply Last reply
                0
                • S SimpleData

                  I have commented it and seems like it's still working. Thank you for your help.

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #8

                  Excellent. Try to remember the 'using' construct, whenever you create anything IDisposable. I don't always, particularly when I'm 'just trying something out' and forgetting nearly always causes me problems. Good Luck! :)

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  1 Reply Last reply
                  0
                  • S SimpleData

                    Hi I have a Windows form called mainwindow in my project. After it completes what it does, it closes and then recreates itself. But as I have noticed lately it recreates but doesnt't close itself. Here is my code:

                    private void RestartAll()
                    {
                    // These are not that important, only to transfer the same properties of the form to the new form.
                    depo.multipleformlocation=this.Location;
                    depo.multipleform = true;
                    depo.ekrangoruntusuadedi = 0;

                    this.Hide();

                    mainwindow _mainwindow = new mainwindow();
                    _mainwindow.ShowDialog();

                    this.Close();

                    // Here is my temporary solution (putting old form's thread to sleep infinitely because it shouldn't keep doing what it does)
                    Thread.Sleep(Timeout.Infinite);
                    }

                    How can I close(destroy) the old form completely and recreate that form? When I don't completely close that old form, because I do this closing and recreating process more than once my App's memory usage gets much higher. Thanks in advance.

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

                    Hi, I don't understand what it is you are doing here. Either this code runs on the GUI thread, and your Sleep(Infinite) hangs it forever, or it runs on some other thread, and it is not allowed to touch any Controls. I am afraid your approach is completely wrong. :confused:

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                    S H 2 Replies Last reply
                    0
                    • L Luc Pattyn

                      Hi, I don't understand what it is you are doing here. Either this code runs on the GUI thread, and your Sleep(Infinite) hangs it forever, or it runs on some other thread, and it is not allowed to touch any Controls. I am afraid your approach is completely wrong. :confused:

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                      S Offline
                      S Offline
                      SimpleData
                      wrote on last edited by
                      #10

                      Well, this method doesn't run on the GUI thread but Control.CheckForIllegalCrossThreadCalls is set to false, so all threads are allowed to touch all the Controls.

                      L D 2 Replies Last reply
                      0
                      • S SimpleData

                        Well, this method doesn't run on the GUI thread but Control.CheckForIllegalCrossThreadCalls is set to false, so all threads are allowed to touch all the Controls.

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

                        SimpleData wrote:

                        so all threads are allowed to touch all the Controls.

                        That is absolutely false. All CheckForIllegalCrossThreadCalls=false; does is disable the checking (returning to the unhealthy situation that existed in .NET 1.0/1.1), it does not ALLOW anything. Controls are not thread-safe and should be touched only by the thread that created them, and since all Controls are linked somehow (through Control collections, Form Z-Order, etc) that basically means the initial/main/GUI thread is the only one that can touch them. Violating this rule may and will result in abnormal app behavior, typically the GUI freezing pretty soon (or after a possibly long while, but failure is guaranteed). :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                        S 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          SimpleData wrote:

                          so all threads are allowed to touch all the Controls.

                          That is absolutely false. All CheckForIllegalCrossThreadCalls=false; does is disable the checking (returning to the unhealthy situation that existed in .NET 1.0/1.1), it does not ALLOW anything. Controls are not thread-safe and should be touched only by the thread that created them, and since all Controls are linked somehow (through Control collections, Form Z-Order, etc) that basically means the initial/main/GUI thread is the only one that can touch them. Violating this rule may and will result in abnormal app behavior, typically the GUI freezing pretty soon (or after a possibly long while, but failure is guaranteed). :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                          S Offline
                          S Offline
                          SimpleData
                          wrote on last edited by
                          #12

                          That's good to know, I didn't know that. I will test my application's stability, maybe it won't be a problem. :)

                          D 1 Reply Last reply
                          0
                          • S SimpleData

                            Well, this method doesn't run on the GUI thread but Control.CheckForIllegalCrossThreadCalls is set to false, so all threads are allowed to touch all the Controls.

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #13

                            Just because the property is there doesn't mean it should be used. I concur with Luc, your approach is completely wrong and unsupportable. I really can't suggest a replacement because it isn't clear what you're trying to accomplish. Also, when you show a form with ShowDialog, you are responsible for calling Dispose on it to free up unmanaged resources before the object goes out of focus and is destroyed. As it stands now, your code leaks system resources, specifically, window handles. Your code, if left running long enough, will eventually crash Windows.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                 2006, 2007, 2008

                            S 1 Reply Last reply
                            0
                            • S SimpleData

                              That's good to know, I didn't know that. I will test my application's stability, maybe it won't be a problem. :)

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              It may pass your tests, but when released into the real world, your code WILL fail. It's just a matter of time before someone out there find the combination of circumstances that will make it happen.

                              A guide to posting questions on CodeProject[^]
                              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                   2006, 2007, 2008

                              1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                Just because the property is there doesn't mean it should be used. I concur with Luc, your approach is completely wrong and unsupportable. I really can't suggest a replacement because it isn't clear what you're trying to accomplish. Also, when you show a form with ShowDialog, you are responsible for calling Dispose on it to free up unmanaged resources before the object goes out of focus and is destroyed. As it stands now, your code leaks system resources, specifically, window handles. Your code, if left running long enough, will eventually crash Windows.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                     2006, 2007, 2008

                                S Offline
                                S Offline
                                SimpleData
                                wrote on last edited by
                                #15

                                What can I do to prevent this?

                                D 1 Reply Last reply
                                0
                                • S SimpleData

                                  What can I do to prevent this?

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #16

                                  Again, we can't really tell you al alternate method without knowing what you're trying to do with this code.

                                  A guide to posting questions on CodeProject[^]
                                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                       2006, 2007, 2008

                                  S 1 Reply Last reply
                                  0
                                  • D Dave Kreskowiak

                                    Again, we can't really tell you al alternate method without knowing what you're trying to do with this code.

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                         2006, 2007, 2008

                                    S Offline
                                    S Offline
                                    SimpleData
                                    wrote on last edited by
                                    #17

                                    I am coding an AntiCheat application which takes screenshots of the game and uploads to the server after the games is closed. When the app is launched, first the login form appears, if the login info is correct, login from closes and mainform that we are talking about appears. App waits for the user to open a supported game. When it's opened it takes screenshots in random intervals. After the game is closed app uploads the taken screenshots. After uploading them in order to start waiting for a new game to open, I close the mainform and recreate it. This recreation process is what we are talking about.

                                    D 1 Reply Last reply
                                    0
                                    • S SimpleData

                                      I am coding an AntiCheat application which takes screenshots of the game and uploads to the server after the games is closed. When the app is launched, first the login form appears, if the login info is correct, login from closes and mainform that we are talking about appears. App waits for the user to open a supported game. When it's opened it takes screenshots in random intervals. After the game is closed app uploads the taken screenshots. After uploading them in order to start waiting for a new game to open, I close the mainform and recreate it. This recreation process is what we are talking about.

                                      D Offline
                                      D Offline
                                      DidiKunz
                                      wrote on last edited by
                                      #18

                                      Why can't you hide the form, instead of close and reopen? Regards: Didi

                                      S 1 Reply Last reply
                                      0
                                      • D DidiKunz

                                        Why can't you hide the form, instead of close and reopen? Regards: Didi

                                        S Offline
                                        S Offline
                                        SimpleData
                                        wrote on last edited by
                                        #19

                                        Because I don't want to keep an extra copy of that form at the back and I need another copy of it.

                                        D 1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          Hi, I don't understand what it is you are doing here. Either this code runs on the GUI thread, and your Sleep(Infinite) hangs it forever, or it runs on some other thread, and it is not allowed to touch any Controls. I am afraid your approach is completely wrong. :confused:

                                          Luc Pattyn [Forum Guidelines] [My Articles]


                                          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                                          H Offline
                                          H Offline
                                          Henry Minute
                                          wrote on last edited by
                                          #20

                                          I noticed the Thread.Sleep after the this.Close(), but as I could not even start to imagine what was, or was supposed to happen. I therefore very deliberately refrained from mentioning it. Now you have gone and stirred the whole thing up again! :)

                                          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                                          L 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