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. How to freeze an application?

How to freeze an application?

Scheduled Pinned Locked Moved C#
tutorialquestion
10 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.
  • A Offline
    A Offline
    Anneke
    wrote on last edited by
    #1

    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

    L O C C 4 Replies Last reply
    0
    • A Anneke

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 is true. regards

      A 1 Reply Last reply
      0
      • A Anneke

        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

        O Offline
        O Offline
        oobimoo
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Anneke

          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

          C Offline
          C Offline
          carbon_golem
          wrote on last edited by
          #4

          This[^] should do the trick.

          “It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra

          C 1 Reply Last reply
          0
          • C carbon_golem

            This[^] should do the trick.

            “It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra

            C Offline
            C Offline
            chaiguy1337
            wrote on last edited by
            #5

            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! -”-”-

            1 Reply Last reply
            0
            • A Anneke

              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

              C Offline
              C Offline
              chaiguy1337
              wrote on last edited by
              #6

              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! -”-”-

              A 1 Reply Last reply
              0
              • L Lost User

                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 is true. regards

                A Offline
                A Offline
                Anneke
                wrote on last edited by
                #7

                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.

                T 1 Reply Last reply
                0
                • C chaiguy1337

                  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! -”-”-

                  A Offline
                  A Offline
                  Anneke
                  wrote on last edited by
                  #8

                  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

                  C 1 Reply Last reply
                  0
                  • A Anneke

                    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

                    C Offline
                    C Offline
                    chaiguy1337
                    wrote on last edited by
                    #9

                    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! -”-”-

                    1 Reply Last reply
                    0
                    • A Anneke

                      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.

                      T Offline
                      T Offline
                      Thomas Stockwell
                      wrote on last edited by
                      #10

                      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

                      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