Closing and recreating a form
-
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.
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.
-
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.
Well, this method doesn't run on the GUI thread but
Control.CheckForIllegalCrossThreadCalls
is set tofalse
, so all threads are allowed to touch all the Controls. -
Well, this method doesn't run on the GUI thread but
Control.CheckForIllegalCrossThreadCalls
is set tofalse
, so all threads are allowed to touch all the Controls.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.
-
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.
That's good to know, I didn't know that. I will test my application's stability, maybe it won't be a problem. :)
-
Well, this method doesn't run on the GUI thread but
Control.CheckForIllegalCrossThreadCalls
is set tofalse
, so all threads are allowed to touch all the Controls.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 -
That's good to know, I didn't know that. I will test my application's stability, maybe it won't be a problem. :)
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 -
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, 2008What can I do to prevent this?
-
What can I do to prevent this?
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 -
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, 2008I 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.
-
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.
-
Because I don't want to keep an extra copy of that form at the back and I need another copy of it.
-
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.
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.”
-
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.”
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. ;PLuc 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.
-
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. ;PLuc 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.
:-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.”
-
:-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.”
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.
-
Because I don't want to keep an extra copy of that form at the back and I need another copy of it.
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 -
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.
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.
-
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, 2008I will try to make it work that way. Thanks.
-
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.
It doesn't close because the
ShowDialog
method blocks the current thread. It means that the rest of theRestartAll
method body is executed after a fresh copy of your mainwindow is closed. Switch toShow()
instead. I think that you can also movethis.Close
above_mainwindow.Show()
.Greetings - Jacek Gajek
-
It doesn't close because the
ShowDialog
method blocks the current thread. It means that the rest of theRestartAll
method body is executed after a fresh copy of your mainwindow is closed. Switch toShow()
instead. I think that you can also movethis.Close
above_mainwindow.Show()
.Greetings - Jacek Gajek
If I do it that way, then no new mainwindow appears.