Question about running a program in a loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Question about running a program in a loop

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Question about running a program in a loop

    Say that you have a simple program with a start and a stop button. Once the start button is start, the program should loop through some code at every new second. Then how can you make the stop button break the loop that is activated with the start button?

    I figured something like this:

    Code:
    bool keepRunning = true;
    int NewSecond;
    int MemSecond;
    
    private void StartButton(object sender, EventArgs e)
            {
    while(keepRunning){
      NewSecond = DateTime.Now.Second;
      if(NewSecond!=MemSecond){
        MemSecond = NewSecond;
        //run the program that's in the loop
       }
      System.Threading.Thread.Sleep(1);
    }
    }
    private void StopButton(object sender, EventArgs e)
            {
    keepRunning = false;
    }

    But then I get an not responding program?

  2. #2
    Join Date
    Jan 2012
    Location
    India
    Posts
    138

    Re: Question about running a program in a loop

    I am not sure , but I think you will need to use a thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center