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 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
                          • H Henry Minute

                            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 Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #21

                            Henry Minute wrote:

                            Now you have gone and stirred the whole thing up again!

                            Yeah, that's me. Always fighting against those silly Thread.Sleep(Timeout.Infinite); statements which waste an entire stack without achieving anything. Sorry for being a tad more radical; "Try commenting it out and see if everything still works..." is not really my style. ;P

                            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 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Henry Minute wrote:

                              Now you have gone and stirred the whole thing up again!

                              Yeah, that's me. Always fighting against those silly Thread.Sleep(Timeout.Infinite); statements which waste an entire stack without achieving anything. Sorry for being a tad more radical; "Try commenting it out and see if everything still works..." is not really my style. ;P

                              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
                              #22

                              :-O

                              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
                              • H Henry Minute

                                :-O

                                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 Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #23

                                NP :beer:

                                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
                                • S SimpleData

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

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

                                  Why?? If your code is written correctly, you shouldn't have to do this at all. The code should just fall back to waiting for a suported game to launch.

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

                                  S 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    NP :beer:

                                    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
                                    #25

                                    I know that that command should not be there but it was a temporary solution and it was working in a way. This shows that this.Close(); doesn't work. I still couldn't figure out why, if it could then there would be no problem.

                                    1 Reply Last reply
                                    0
                                    • D Dave Kreskowiak

                                      Why?? If your code is written correctly, you shouldn't have to do this at all. The code should just fall back to waiting for a suported game to launch.

                                      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
                                      #26

                                      I will try to make it work that way. Thanks.

                                      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
                                        Lutoslaw
                                        wrote on last edited by
                                        #27

                                        It doesn't close because the ShowDialog method blocks the current thread. It means that the rest of the RestartAll method body is executed after a fresh copy of your mainwindow is closed. Switch to Show() instead. I think that you can also move this.Close above _mainwindow.Show().

                                        Greetings - Jacek Gajek

                                        S 1 Reply Last reply
                                        0
                                        • L Lutoslaw

                                          It doesn't close because the ShowDialog method blocks the current thread. It means that the rest of the RestartAll method body is executed after a fresh copy of your mainwindow is closed. Switch to Show() instead. I think that you can also move this.Close above _mainwindow.Show().

                                          Greetings - Jacek Gajek

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

                                          If I do it that way, then no new mainwindow appears.

                                          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