How to freeze an application?
-
Hi I've made a graphic simulation with a timer. But now I want to stop the simulation (but not disappear) only freeze the simulation. I've already tried to use a do-while-loop but then the application crashes. I've also tried to use a if-loop and in the loop set the length (that the application have to drawn) to zero but it doesn't freeze. I've also tried to use in a if-loop, to call another function which doesn't do anything but this doesn't freeze the application. Also I did this in a do-while-loop but then it crashes. Does someone knows an answer and perhaps knows an example that I can understand what you're trying to say. PS: The simulation is shown in a pictureBox
-
Hi I've made a graphic simulation with a timer. But now I want to stop the simulation (but not disappear) only freeze the simulation. I've already tried to use a do-while-loop but then the application crashes. I've also tried to use a if-loop and in the loop set the length (that the application have to drawn) to zero but it doesn't freeze. I've also tried to use in a if-loop, to call another function which doesn't do anything but this doesn't freeze the application. Also I did this in a do-while-loop but then it crashes. Does someone knows an answer and perhaps knows an example that I can understand what you're trying to say. PS: The simulation is shown in a pictureBox
Graphic simulations often depend on calculations done in an Update loop based on the time that elapsed since the last frame. If you set the elapsed time in your update loop to zero (i.e.: "no time elapsed since the last frame"), you can bring your simulation to a halt. Another possibility would be to set a flag
bool paused = true
and stop any updates whenever this flag istrue
. regards -
Hi I've made a graphic simulation with a timer. But now I want to stop the simulation (but not disappear) only freeze the simulation. I've already tried to use a do-while-loop but then the application crashes. I've also tried to use a if-loop and in the loop set the length (that the application have to drawn) to zero but it doesn't freeze. I've also tried to use in a if-loop, to call another function which doesn't do anything but this doesn't freeze the application. Also I did this in a do-while-loop but then it crashes. Does someone knows an answer and perhaps knows an example that I can understand what you're trying to say. PS: The simulation is shown in a pictureBox
1. With a System.Timers.Timer just use the Stop() method to stop the timer. 2. With a System.Threading.Timer, just don't use this timer (why? read documentation). Replace with a System.Timers.Timer 3. For good performance forget timers. Force rendering into app's main loop (as Greeeg mentioned)
modified on Wednesday, August 27, 2008 8:03 AM
-
Hi I've made a graphic simulation with a timer. But now I want to stop the simulation (but not disappear) only freeze the simulation. I've already tried to use a do-while-loop but then the application crashes. I've also tried to use a if-loop and in the loop set the length (that the application have to drawn) to zero but it doesn't freeze. I've also tried to use in a if-loop, to call another function which doesn't do anything but this doesn't freeze the application. Also I did this in a do-while-loop but then it crashes. Does someone knows an answer and perhaps knows an example that I can understand what you're trying to say. PS: The simulation is shown in a pictureBox
-
Cool. :P
“Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’) Built with home-grown CodeProject components! -”-”-
-
Hi I've made a graphic simulation with a timer. But now I want to stop the simulation (but not disappear) only freeze the simulation. I've already tried to use a do-while-loop but then the application crashes. I've also tried to use a if-loop and in the loop set the length (that the application have to drawn) to zero but it doesn't freeze. I've also tried to use in a if-loop, to call another function which doesn't do anything but this doesn't freeze the application. Also I did this in a do-while-loop but then it crashes. Does someone knows an answer and perhaps knows an example that I can understand what you're trying to say. PS: The simulation is shown in a pictureBox
As the others suggested, the answer is to stop the actual simulation from taking place by means of something like an internal variable or a timer setting. Simply freezing the application will cause Windows to think it has hung, and which will display the unresponsive program dialog (which I am guessing is what you are calling "crashing"). It will also not allow you to _un_freeze it again later. You might also look into performing the simulation on a separate thread (though remember you will still have to update the picturebox on the main thread), because then you will just be able to pause and resume the simulation thread at will, and without chewing up CPU cycles in an infinite loop. :)
“Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’) Built with home-grown CodeProject components! -”-”-
-
Graphic simulations often depend on calculations done in an Update loop based on the time that elapsed since the last frame. If you set the elapsed time in your update loop to zero (i.e.: "no time elapsed since the last frame"), you can bring your simulation to a halt. Another possibility would be to set a flag
bool paused = true
and stop any updates whenever this flag istrue
. regards -
As the others suggested, the answer is to stop the actual simulation from taking place by means of something like an internal variable or a timer setting. Simply freezing the application will cause Windows to think it has hung, and which will display the unresponsive program dialog (which I am guessing is what you are calling "crashing"). It will also not allow you to _un_freeze it again later. You might also look into performing the simulation on a separate thread (though remember you will still have to update the picturebox on the main thread), because then you will just be able to pause and resume the simulation thread at will, and without chewing up CPU cycles in an infinite loop. :)
“Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’) Built with home-grown CodeProject components! -”-”-
But how can I do this in this code? [code] private void pictureBox1_Paint(object sender, PaintEventArgs e) { // opvragen van de burst die momenteel gedaan is // dit kan via een methode in de Controller te steken // die controlleerd welke knop (FCFS-SJF) ingedrukt werd // in deze klasse wordt er dan gekeken naar de eindeburst // Dat moet dan doorgestuurd worden naar hier. Graphics g = e.Graphics; String algoritme = cmbSchedule.SelectedItem.ToString(); int burstlengte = 0; switch (algoritme) { case "FCFS": //burstlengte = Controller.GetInstance(ProcessList).Simulatie(ProcessList[0].lijstvanBursts[0]); burstlengte = Controller.GetInstance(ProcessList).Hoeveelburst(ProcessList[0].lijstvanBursts[0].Lengte); foreach (Proces p in ProcessList) { foreach (Burst b in p.lijstvanBursts) { if (Controller.GetInstance(ProcessList).Teller > p.PAankomst + b.Lengte) { // No simulation anymore //burstlengte = 0; //pictureBox1.Update(); } //do //{ // eind(); //} while (Controller.GetInstance(ProcessList).Teller > (p.PAankomst + b.Lengte) || Controller.GetInstance(ProcessList).Teller == (p.PAankomst + b.Lengte)); } } break; case "SJF": //burstlengte = SJF.getInstance().GeordendeLijst[0].lijstvanBursts[0].Lengte; //burstlengte = Controller.GetInstance(ProcessList).Hoeveelburst(SJF.getInstance().GeordendeLijst[0].lijstvanBursts[0].Lengte); break; } // Laat de simulatie niet verder gaan als de burst gedaan is //if (Controller.GetInstance(ProcessList).Teller > burstlengte + ProcessList[0].PAankomst) //{ // burstlengte = 0; //} //int burstlengte = Controller.GetInstance(ProcessList).Simulatie(ProcessList[0].lijs
-
But how can I do this in this code? [code] private void pictureBox1_Paint(object sender, PaintEventArgs e) { // opvragen van de burst die momenteel gedaan is // dit kan via een methode in de Controller te steken // die controlleerd welke knop (FCFS-SJF) ingedrukt werd // in deze klasse wordt er dan gekeken naar de eindeburst // Dat moet dan doorgestuurd worden naar hier. Graphics g = e.Graphics; String algoritme = cmbSchedule.SelectedItem.ToString(); int burstlengte = 0; switch (algoritme) { case "FCFS": //burstlengte = Controller.GetInstance(ProcessList).Simulatie(ProcessList[0].lijstvanBursts[0]); burstlengte = Controller.GetInstance(ProcessList).Hoeveelburst(ProcessList[0].lijstvanBursts[0].Lengte); foreach (Proces p in ProcessList) { foreach (Burst b in p.lijstvanBursts) { if (Controller.GetInstance(ProcessList).Teller > p.PAankomst + b.Lengte) { // No simulation anymore //burstlengte = 0; //pictureBox1.Update(); } //do //{ // eind(); //} while (Controller.GetInstance(ProcessList).Teller > (p.PAankomst + b.Lengte) || Controller.GetInstance(ProcessList).Teller == (p.PAankomst + b.Lengte)); } } break; case "SJF": //burstlengte = SJF.getInstance().GeordendeLijst[0].lijstvanBursts[0].Lengte; //burstlengte = Controller.GetInstance(ProcessList).Hoeveelburst(SJF.getInstance().GeordendeLijst[0].lijstvanBursts[0].Lengte); break; } // Laat de simulatie niet verder gaan als de burst gedaan is //if (Controller.GetInstance(ProcessList).Teller > burstlengte + ProcessList[0].PAankomst) //{ // burstlengte = 0; //} //int burstlengte = Controller.GetInstance(ProcessList).Simulatie(ProcessList[0].lijs
First of all, move all of that outside of PictureBox.Paint and into a separate thread that modifies a shared (synchronized) bitmap image. Then run the above and either send events to the main thread to update the picture box with the current state of the bitmap at various intervals, or do this on a timer. Just be sure to use lock() before accessing the shared bitmap on either thread. In PictureBox.Paint just draw the shared bitmap to the PictureBox. Now when you pause the simulation, the picturebox should no longer receive signals to update, or if you put it on a separate timer, you should also pause that timer when you pause the simulation. Logan
“Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’) Built with home-grown CodeProject components! -”-”-
-
Thank you for your reply. Is there also an oposit of Update()? Something like .Pause()? I've tried somethings out but they aren't right.
Those methods handle completely different functions then what you may thinking. Update() or Invalidate() are used to refresh the entire form based on the information contained in the OnPaint event. Their is no way to pause the OnPaint event unless you write your own graphic logic. One of my articles Basics of Falling Blocks in VB2005[^] has been able help some people understand how the painting system works in WinForms as well as how it should be thought of.
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog