Reputation: 263
Good day, it can be the easy question (for everyone) but I don't know how to solve that.
Simple question - simple answer.
If I have this code:
public void test()
{
for (int i = 0; i < 10000; i++)
{
label1.Text = i.ToString();
}
}
If I will call the test method, it writes to me 9999 (surely). I need show on label the variable i every second of this loop. I do not want to use a timer - but you can show me a solution with Timer.
What is the easiest way to show LOOP? Excuse me for my English, nice day.
Upvotes: 0
Views: 4829
Reputation: 13743
Not good way but you can use:
public void test()
{
for (int i = 0; i < 10000; i++)
{
System.Threading.Thread.Sleep(1000);
label1.Text = i.ToString();
}
}
Upvotes: 0
Reputation: 82267
sweet and simple
DateTime start= DateTime.Now;
for(int i = 0; i < 10000; i++)
{
DateTime end = DateTime.Now;
if( start.Second != end.Second )
{
start = end;
label1.Text = i.ToString();
}
}
EDIT
This will allow you see the difference. Set a breakpoint after the loop runs and you will see the array of iz filled with i values. The first one on my machine was at 150000
List<string> iz = new List<string>();
DateTime start = DateTime.Now;
for (int i = 0; i < 10000000; i++)
{
int zz = 5000;
int yy = zz / (1 +i);
zz = yy * i;
DateTime end = DateTime.Now;
if (start.Second != end.Second)
{
start = end;
iz.Add(i.ToString());
}
}
Upvotes: 0
Reputation: 6293
Use BackgroundWoker
class, and it's method ReportProgress
, do get desired behaviour without locking UI.
void backgroundWoker_DoWork(....)
{
for (int i = 0; i < 10000; i++)
{
backgroundWoker.ReportProgress(0,i.ToString());
Thread.Sleep(1000);
}
}
void backgroundWoker_ProgressChanged(....)
{
label1.Text = e.UserState.ToString();
}
Upvotes: 1