Search code examples
c#winformsdelaytimedelay

Time Delay for operations using WinForm App in c#


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?


Solution

  •     var thread = new System.Threading.Thread(p =>
        {
            lock (YourTabControl)
            {
                Action action = () =>
                {
                    addTab(url);
                };
                this.Invoke(action);
                if(url.Host == "google")
                    System.Threading.Thread.Sleep(5000);
            }
        });
        thread.Start();