locked
C# for loop stop and continue RRS feed

  • Question

  • I want stop for loop and then type value in text box,  then again continue for loop from that stop line. How i do this????
    Saturday, February 13, 2016 3:21 AM

Answers

  • Hi Isuru Aravinda,

    Because I don't know in what langugage or platform you are developing this App, I will type it literally with C#, I suggest you to make one global variable: 

    bool status = false;


    and asign event to your text box:

    private void YourTextBox_TextChanged(object sender, RoutedEventArgs e)
    {
        status = true;
    }


    and than in your for loop you can check if status is true or false:

    for(int i = 0; i < someYourValue; i++){
    
       // other code...
    
       if(status){
    
         i = someYourValue;
       }
    
    }


    This is very simple example, you can tweak about it, and make it to work best for you, but this is my first idea when I read you question.

    Best regards!


    (If this was helpful for you, vote for it and propose it as an answer)

    Saturday, February 13, 2016 10:59 AM

All replies

  • Hi Isuru Aravinda,

    Because I don't know in what langugage or platform you are developing this App, I will type it literally with C#, I suggest you to make one global variable: 

    bool status = false;


    and asign event to your text box:

    private void YourTextBox_TextChanged(object sender, RoutedEventArgs e)
    {
        status = true;
    }


    and than in your for loop you can check if status is true or false:

    for(int i = 0; i < someYourValue; i++){
    
       // other code...
    
       if(status){
    
         i = someYourValue;
       }
    
    }


    This is very simple example, you can tweak about it, and make it to work best for you, but this is my first idea when I read you question.

    Best regards!


    (If this was helpful for you, vote for it and propose it as an answer)

    Saturday, February 13, 2016 10:59 AM
  • Hi Isuru Aravinda,

    Please Provide more information 

    1-How do you want to stop/pause for loop (by Clicking a button or some condition)

    2-How do you resume/start for loop (by Clicking button or Pressing enter after you entered text)

    It's obvious you need to run your fool loop in second thread to be able stop it from UI and you UI be responsive when looping.

    How to implement async operation depend on type of Application:

    1- Console Applicatio

    2- Windows WPF Application

    3- Universal Application

    4- WinForm Application

    You need to provide more information

    Thanks,


    • Edited by Hamid Naeemi Saturday, February 13, 2016 11:18 AM
    Saturday, February 13, 2016 11:17 AM