Lets Say My code is:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
// I WANT TO DELAY 5 SECONDS HERE. I WANT TO VISIT FACEBOOK.COM AFTER 5 SECONDS AUTOMATICALLY. WHAT CODE SHOULD I WRITE?
webBrowser1.Navigate("http://facebook.com");
}
HELP ME PLEASE. CHEERS
Loading
Csharp NinjaPosted Feb 1, 2012, 7:14 PM
Sam HobbsPosted Jan 28, 2012, 10:09 PM
The first thing I suggest doing is to create a handler for the DocumentComplete event of the WebBrowser control. The DocumentComplete event is used so often that it is the default event for the WebBrowser control. If you double-click the WebBrowser control in the design view of Visual Studio then it will automatically (by default) create a DocumentComplete event hanbdler for the control. I assume you want to delay so that IE has a chance to load Google but the DocumentComplete event will do that more precisely.
If you still need to delay for 5 seconds, then use a timer. You can use the toolbox to drag and drop a timer to your form. Then set the interval to 5 and create a tick event handler. Set the state to disabled so the timer does not start until you start it. After navigating to Google, start the timer. In the tick event handler, stop the timer then navigate to Facebook.
Sarah ReynoldsPosted Jan 27, 2012, 8:31 PM
public static DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor)
{
System.DateTime ThisMoment = System.DateTime.Now;
System.TimeSpan duration = new System.TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor);
System.DateTime AfterWards = ThisMoment.Add(duration);
while (AfterWards >= ThisMoment)
{
System.Windows.Forms.Application.DoEvents();
ThisMoment = System.DateTime.Now;
}
return System.DateTime.Now;
}
Then call it like this:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
PauseForMilliSeconds(8000); //8 second delay
webBrowser1.Navigate("http://facebook.com");
}
This works for me but if anyone else can see problems with this or have a better solution I'd also be interested.
Sarah