Time Delay for operations using WinForm App in c#

Jasper picture Jasper · Nov 23, 2011 · Viewed 9.5k times · Source

I have a problem with a Windows Form Application in C# Inside This application there is a Tabbed Browser. Now i want that in a loop (where i do some operation to calculate the next url) i could add a delay between AddTab.

I.E

 foreach(Urls url in Links){

        // Do something with url
         if(url.Host == "google"){ //if host is google until start the 
                                   // next AddTab i would wait 5 SEC
             addTab(url);
           //Here i Tried to use Sleep Thread , While(TimeCur-TimePre) {} but i always 
           // get Freezing and the other Tabs dont continue the loading
             Wait 5 Seconds in somehow
         }
}

As i wrote i dont want use Thread Sleep becouse it freezes My Form Application. I already used the time trick with TimeCur-TimeSaved+(x Second) but this freezes the form too.

I saw many topics where people say to use Timers so I tried to use them(without any result, maybe i wrong something)

What can i add in this loop to delay the opening between the AddTab without freezing anything?

Answer

Russell picture Russell · Nov 23, 2011

Use a Windows Forms timer (from the toolbox). In properties, set the interval setting. Then double click on it on the form designer. This will create an event handler, which will be fired (in a separate thread) when the interval time elapses. You can do your work here, and the form will remain active.