(Read Title)

I honestly have no idea how to do this. I've tried diagnostics.stopwatch, system.timespan , and system.datetime, but I can't wrap my head around how I would do this. Any suggestions?

Recommended Answers

All 2 Replies

You could use the thread Sleep method, or you could use a Timer.

I agree with ddanbe; If you are comfortable with threadign then move the process to a seperate thread from UI and use sleep to pause it between iterations.

Otherwise, you can put the loop into a timers tick event. You would need to amend the way you write your loop though:

//instead of:
for (int i = 0; i < 10; i++)
{
   //do stuff
}

you would need something like:

private int LoopCounter;

private void Button_Click(object sender, EventArgs e)
{
    LoopCounter = 0;
    Timer.Interval = 1000;
    Timer.Start();
}
private void Timer_Tick (object sender, EventArgs e)
{
    if(LoopCounter < 10)
    {
        //do stuff
    }
    else
    {
        Timer.Stop();
    }

    LoopCounter++;
}

It still has all the aspects of your for loop but because you need to increment the counter outside the code block you split down the logic of creating your counter, checking the condition and incrementing the counter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.