Jump to content

Show loop's current situation while loop continuing process?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
uls

uls

    Newbie

  • Members
  • Pip
  • 4 posts
I have a code something like that;

for (int i = 0; i < 1000; i++)

{

    label1.Text = i.ToString();

}

I want to show the numbers in the label, but the program freezing for a moment when the loop started, then shows the last number (999). How can i handle that? I want too see numbers counting. I tried sleep() but still same.

Edited by uls, 07 November 2010 - 09:32 AM.


#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
How long do you think it takes your computer to count to 1000? Let's say your loop take 2 clock cycles per iteration (increment i, store i) and you have a 2Ghz CPU. So we have 2000 clock cycles with a computer that is doing 2,000,000,000 cycles per second. That works out to be 1 millionth of a second. So you aren't going to see much.

Given that, if you really want to watch it count, create a timer object and increment the lable1.Text in the event trigger. That way you can set the speed at which you want it to increment.

#3
uls

uls

    Newbie

  • Members
  • Pip
  • 4 posts

Momerath said:

How long do you think it takes your computer to count to 1000? Let's say your loop take 2 clock cycles per iteration (increment i, store i) and you have a 2Ghz CPU. So we have 2000 clock cycles with a computer that is doing 2,000,000,000 cycles per second. That works out to be 1 millionth of a second. So you aren't going to see much.

Given that, if you really want to watch it count, create a timer object and increment the lable1.Text in the event trigger. That way you can set the speed at which you want it to increment.

But i used sleep(1000) command after "label1.Text = i.ToString();", it didn't show. I think i have to use backgroundWorker. But i don't have enough information about that. I can send integers, but i need to send strings and arrays. I didn't get the logic exactly.

Here is my code but it's sending just integers;


        private void Form1_Load(object sender, System.EventArgs e)

        {

            backgroundWorker1.RunWorkerAsync();

        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

        {

            for (int i = 0; i < 100; i++)

            {

            backgroundWorker1.ReportProgress(i);

            Thread.Sleep(100);

            }

        }


        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

        {

            progressBar1.Value = e.ProgressPercentage;

            box.Text = e.ProgressPercentage.ToString();

        }




#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Add the line box.Refresh() to the progress changed method:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {

    progressBar1.Value = e.ProgressPercentage;

    box.Text = e.ProgressPercentage.ToString();

    box.Refresh();

}


#5
uls

uls

    Newbie

  • Members
  • Pip
  • 4 posts

Momerath said:

Add the line box.Refresh() to the progress changed method:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
    progressBar1.Value = e.ProgressPercentage;
    box.Text = e.ProgressPercentage.ToString();
    box.Refresh();
}

I am embarrassed. Really thank you.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users