Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time Delay for operations using WinForm App in c# [duplicate]

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?

like image 718
Jasper Avatar asked Oct 27 '25 06:10

Jasper


2 Answers

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.

like image 166
Russell Avatar answered Oct 29 '25 22:10

Russell


    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();
like image 33
Reza ArabQaeni Avatar answered Oct 29 '25 20:10

Reza ArabQaeni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!