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. causing the computer to freeze

causing the computer to freeze

Scheduled Pinned Locked Moved C#
csharpmobilevisual-studiodata-structuresdebugging
8 Posts 5 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.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?

    L OriginalGriffO realJSOPR 0 4 Replies Last reply
    0
    • C Calin Negru

      While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?

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

      With a "stack overflow", even the debugger can't continue.

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      C 1 Reply Last reply
      0
      • C Calin Negru

        While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        A while loop isn't an error as far as the system is concerned - it just means that your display can't be updated and user input is ignored until the loop exist and the event handler is finished. The system doesn't "know" how long you will be looping for and has to assume that you intended that to happen! Unbounded recursion is different: each recursive call uses up an amount of some special memory called the stack - and when that runs out (and it does, pretty quickly) you app has to stop running because it can't execute any more code; it needs the stack to do anything. So it throws an exception - Stack overflow - which the debugger picks up for you and stops you app so you can see what is happening. You shouldn't use big loops in event handlers - they should be moved to a second thread so that the UI thread can continue to update the display and deal with user input. Have a look here: BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        C 1 Reply Last reply
        0
        • C Calin Negru

          While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          A while loop without some sort of exit will cause the application to freeze, but will leave the OS alone (as long as the stack is filled up, or a some sort of min/max value isn't reached). This will execute forever, freezing the app, but leaving the OS responsive:

          public void MethodX(string x)
          {
          while (true)
          {
          }
          }

          This will eventually crash the app with a stack overflow exception:

          ...
          MethodX("");
          ...

          public void MethodX(string x)
          {
          while (true)
          {
          MethodX("1234567890");
          }
          }

          This will run forever:

          public void MethodY()
          {
          int x = 0;
          while (true)
          {
          x = x + 100000;
          }
          }

          This will eventually crash the app with an OutOfMemoryException:

          ...
          MethodZ("1234567890");
          ...

          private static void MethodZ(string x)
          {
          while(true)
          {
          x = x + x;
          Console.WriteLine(x.Length.ToString());
          }
          }

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            A while loop isn't an error as far as the system is concerned - it just means that your display can't be updated and user input is ignored until the loop exist and the event handler is finished. The system doesn't "know" how long you will be looping for and has to assume that you intended that to happen! Unbounded recursion is different: each recursive call uses up an amount of some special memory called the stack - and when that runs out (and it does, pretty quickly) you app has to stop running because it can't execute any more code; it needs the stack to do anything. So it throws an exception - Stack overflow - which the debugger picks up for you and stops you app so you can see what is happening. You shouldn't use big loops in event handlers - they should be moved to a second thread so that the UI thread can continue to update the display and deal with user input. Have a look here: BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #5

            Thanks Griff that`s a good theoretical explanation

            OriginalGriffO 1 Reply Last reply
            0
            • C Calin Negru

              Thanks Griff that`s a good theoretical explanation

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              You're welcome!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • L Lost User

                With a "stack overflow", even the debugger can't continue.

                "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                C Offline
                C Offline
                Calin Negru
                wrote on last edited by
                #7

                with the right pair of boots and equipment for hostile environment you never know

                1 Reply Last reply
                0
                • C Calin Negru

                  While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze? Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?

                  0 Offline
                  0 Offline
                  0x01AA
                  wrote on last edited by
                  #8

                  Only for the record: In a while loop you can call Application.DoEvents() from time to time. Not really nice but it will not completely freeze the app ;)

                  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