none
App that adds 1 for every for, waiting 2 seconds every loop C# RRS feed

  • Question

  • I'm trying to make an app that loops 10 times, and everytime it loops, it will wait for 2 seconds for the next loop. also it would add 1 to an int variable.

    But the problem is: everytime it starts, the int variable resets.

    Here's my code:

    public Form1()
    
            {
                InitializeComponent();
                this.progressBar1.Value = 0;
                this.timer1.Interval = 100;
                this.timer1.Enabled = true;
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                Random random = new Random();
    
                if (this.progressBar1.Value != 100)
                {
                    int resultj = 0;
                    int j = random.Next(1, 20);
    
    
                    for (int i = 0; i < 10; i++)
                    {
                        label1.Text = (resultj);
                        resultj++;
                                           
                    }
                    this.progressBar1.Value++;
                }
            }

    Saturday, November 24, 2012 5:52 PM

Answers

  • "I'm trying to make an app that loops 10 times, and everytime it loops, it will wait for 2 seconds for the next loop. also it would add 1 to an int variable."

    If you set the timer interval to 2000 and your int variable is the value property of the progress bar, this portion of your code will suffice:

        public Form1()
        {
          InitializeComponent();
          this.progressBar1.Value = 0;
          this.timer1.Interval = 2000;
          this.timer1.Enabled = true;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {      
          this.progressBar1.Value += 10;
          if (this.progressBar1.Value == 100) timer1.Stop();    
        }

    Saturday, November 24, 2012 8:24 PM

All replies

  • Perhaps this line:

    int kenzo = random.Next(1, 20);
    

    should actually be this:

    resultj = random.Next(1, 20);
    


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Saturday, November 24, 2012 6:12 PM
  • my code was wrong, I've fixed it but it's still wrong :/
    Saturday, November 24, 2012 6:35 PM
  •  Here's another necessary change; from this:

             this.timer1.Enabled = true;
         }
    
         private void timer1_Tick(object sender, EventArgs e)
         {
             Random random = new Random();
    
             if (this.progressBar1.Value != 100)
    

    to this:

             this.timer1.Enabled = true;
             random = new Random();
         }
    
         Random random;
    
         private void timer1_Tick(object sender, EventArgs e)
         {
             if (this.progressBar1.Value != 100)
    
    This avoids assigning a new Random on every clock tick.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Saturday, November 24, 2012 8:21 PM
  • "I'm trying to make an app that loops 10 times, and everytime it loops, it will wait for 2 seconds for the next loop. also it would add 1 to an int variable."

    If you set the timer interval to 2000 and your int variable is the value property of the progress bar, this portion of your code will suffice:

        public Form1()
        {
          InitializeComponent();
          this.progressBar1.Value = 0;
          this.timer1.Interval = 2000;
          this.timer1.Enabled = true;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {      
          this.progressBar1.Value += 10;
          if (this.progressBar1.Value == 100) timer1.Stop();    
        }

    Saturday, November 24, 2012 8:24 PM