Hello,
I create a delayer for something but it dont work right, that i want is
1, Load Form
2, Draw a text in TabPage
3, Show a text in tabpage for 5 seconds
4, when delay end then add WebBrowser Control to TabPage

PS: I forgot notice, when use Thread.Sleep(x) method, it make all programm wait... thats lol;p

Example, if put delay 1 minute you cant do any other action in programm...
i want delay only on TabPage...
any link to see how it work?

This is my code

// Controls we need...
        WebBrowser wBrowser = new WebBrowser();
        Graphics g;
        
        // Delay Function
        private void Delay(int ms)
        {
            g = TMPage.CreateGraphics();
            g.DrawString("Please wait while programm is checking for possible " +
                        "internet connection...", new Font("Segoe UI", 10), Brushes.Gray, new Point(100, 100));

            //WebBrowser Setings
            wBrowser.Url = new Uri("http://google.gr");
            wBrowser.Dock = DockStyle.Fill;

            // Wait a bit...
            Thread.Sleep(ms);

            TMPage.Controls.Add(wBrowser);

        }

        private void Form1_Load(object sender, EventArgs e) {
            // Delay 3 Seconds before show WebBrowser control...
            Delay(3000);

Edited 5 Years Ago by Panathinaikos22: n/a

You need to use a Timer component.

Change your Delay method to only show the text (or add the text bit directly to Form1_Load).
Add a Timer component to your form.
Set its Interval to 3000.
Set the Enabled property to True (it will now start when form is loaded).
Double click to create the Tick event handler.
Set timer Enabled to false at begining of handler (so it won't tick again).
Put code in the Tick handler to add the WebBrowser to form.

Timer would sulrey be a better option then using Thread.Sleep() method.
As nick already explained, you have to create a new instance of a Timer (Windows.Forms.Timer class!), and in the for load event (or in constructor), subscribe to a Tick event, which will be fired (after a time declated in the Interval property).
As said, set Interval property to 3000 (this is 3 sec), and Start it (use start method). After 3 seconds Tick event will be rised. There put your code inside, or call another method (which is a better option), and Stop your timer, so it will no rise this same event again (if you dont need to of course).
thats it.

This article has been dead for over six months. Start a new discussion instead.