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. Stop while loop

Stop while loop

Scheduled Pinned Locked Moved C#
11 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.
  • Y yesu prakash

    How can i stop an while loop by clicking a button? eg: A while loop is executing, i want to stop the while loop from outside like a button click Thankyou YPKI

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

    you must put that loop in a new thread. then use a flag, like _isButtonClicked. If yes, then break.

    Y 1 Reply Last reply
    0
    • Y yesu prakash

      How can i stop an while loop by clicking a button? eg: A while loop is executing, i want to stop the while loop from outside like a button click Thankyou YPKI

      D Offline
      D Offline
      Dino Mulahusic
      wrote on last edited by
      #3

      I don't know do you want to break immediately, but something like this may help

      bool stop = false;

      while(!stop)
      {
      ..dosomething
      }

      or

      while(true)
      {
      if(stop)
      break;
      }

      and on your button_click
      {
      stop = true;
      }

      hope it helps

      1 Reply Last reply
      0
      • Y yesu prakash

        How can i stop an while loop by clicking a button? eg: A while loop is executing, i want to stop the while loop from outside like a button click Thankyou YPKI

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #4

        A bit vague. Is your while loop executing in a background thread? If it's executing on your GUI thread and your not calling Application.DoEvents it won't ever break!

        Regards, Rob Philpott.

        Y 1 Reply Last reply
        0
        • R Rob Philpott

          A bit vague. Is your while loop executing in a background thread? If it's executing on your GUI thread and your not calling Application.DoEvents it won't ever break!

          Regards, Rob Philpott.

          Y Offline
          Y Offline
          yesu prakash
          wrote on last edited by
          #5

          while loop is runnung normally, not in thread. how can i stop using Application.DoEvents

          R 1 Reply Last reply
          0
          • L Lost User

            you must put that loop in a new thread. then use a flag, like _isButtonClicked. If yes, then break.

            Y Offline
            Y Offline
            yesu prakash
            wrote on last edited by
            #6

            how to put a while loop in a thread?

            D 1 Reply Last reply
            0
            • Y yesu prakash

              while loop is runnung normally, not in thread. how can i stop using Application.DoEvents

              R Offline
              R Offline
              Rob Philpott
              wrote on last edited by
              #7

              Create a boolean member:

              private bool _running = true;

              In the button click event set it to false:

              _running = false;

              Make your while loop look like this

              while (_running)
              {
              Application.DoEvents();
              // your stuff here
              }

              Regards, Rob Philpott.

              D 1 Reply Last reply
              0
              • Y yesu prakash

                how to put a while loop in a thread?

                D Offline
                D Offline
                dojohansen
                wrote on last edited by
                #8

                The easiest way to put it in a background thread is to make sure you have a method signature that take one parameter of type object and then use ThreadPool.QueueUserWorkItem(new WaitCallback(method)) - "method" simply being the name of the method you wish to call.

                bool stop;
                void method(object dummy)
                {
                while (!stop)
                {
                ...
                }
                }

                However, be aware that the code in method should not use any references to any controls. By default, doing so will result in an exception. You can make this go away by setting Control.CheckForIllegalCrossThreadCalls to false, but it's not a good practice to do so, I think because the controls aren't designed to be thread-safe. Depending on what you're doing, you may or may not decide that you don't care and just use the controls from the background thread anyway. I've done it many times (in apps for my own use only, where I don't really care if this one day leads to some odd behavior of a button because two threads tried to update it's text property at once, resulting in something weird and maybe beautiful) and I haven't had any problems with it, but there is undoubtedly a good reason why Microsoft decided to disallow this by default.

                1 Reply Last reply
                0
                • R Rob Philpott

                  Create a boolean member:

                  private bool _running = true;

                  In the button click event set it to false:

                  _running = false;

                  Make your while loop look like this

                  while (_running)
                  {
                  Application.DoEvents();
                  // your stuff here
                  }

                  Regards, Rob Philpott.

                  D Offline
                  D Offline
                  Deresen
                  wrote on last edited by
                  #9

                  Problem with this is that there won't be any click events, because it won't check this. You can better use a thread and in this thread you put your code. Something like this (out of my head):

                  private bool _running = true;

                  public static void main(args[])
                  {
                  Thread t = new Thread(threadFunction);
                  t.Start();
                  }

                  public void threadFunction()
                  {
                  while(_running)
                  {
                  // your stuff here;
                  }
                  }

                  public void Button1_Clicked(object sender, EventArgs e)
                  {
                  _running = false;
                  }

                  Don't know if the 'thread' part is correct, the rest is ok ;P

                  R 1 Reply Last reply
                  0
                  • D Deresen

                    Problem with this is that there won't be any click events, because it won't check this. You can better use a thread and in this thread you put your code. Something like this (out of my head):

                    private bool _running = true;

                    public static void main(args[])
                    {
                    Thread t = new Thread(threadFunction);
                    t.Start();
                    }

                    public void threadFunction()
                    {
                    while(_running)
                    {
                    // your stuff here;
                    }
                    }

                    public void Button1_Clicked(object sender, EventArgs e)
                    {
                    _running = false;
                    }

                    Don't know if the 'thread' part is correct, the rest is ok ;P

                    R Offline
                    R Offline
                    Rob Philpott
                    wrote on last edited by
                    #10

                    Actually, its perfectly valid. The Application.DoEvents keeps the message pump pumping and is the way mutitasking used to work in the Windows 3.x world. Admittedly, I prefer your way of doing it but the gentleman suggested he didn't want to use background threads.

                    Regards, Rob Philpott.

                    D 1 Reply Last reply
                    0
                    • R Rob Philpott

                      Actually, its perfectly valid. The Application.DoEvents keeps the message pump pumping and is the way mutitasking used to work in the Windows 3.x world. Admittedly, I prefer your way of doing it but the gentleman suggested he didn't want to use background threads.

                      Regards, Rob Philpott.

                      D Offline
                      D Offline
                      Deresen
                      wrote on last edited by
                      #11

                      I've never used the Application.DoEvents() function. Didn't even know the existance of it. With that in my mind, I totaly agree with you.

                      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