如何在C#中的操作之间暂停

时间:2016-03-03 09:15:07

标签: c# vb.net

在这个例子中,我使用C#

在VB中使用1个按钮和Web浏览器

我只是想按一下按钮然后让它去bing,等待2秒,然后去google。我在尝试时看到的每种方法总是在开始时暂停或暂停,而不是在导航之间。这就是我所拥有的。提前致谢。

public void  button1_Click(object sender, EventArgs e)
    {

        WebBrowser1.Navigate("http://www.bing.com");


        Thread.sleep(2000);


        WebBrowser1.Navigate("http://www.google.com");

    }

1 个答案:

答案 0 :(得分:2)

订阅DocumentCompleted活动,然后导航到第二页:

private void LoadPages()
{
     WebBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser1_DocumentCompleted);
     WebBrowser1.Navigate("http://www.bing.com");
}

void WebBrowser1_DocumentCompleted(object sender,  WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser1.Navigate("http://www.google.com");

    // Might want to dispose of the webbrowser instance or else
    // this event will fire again for the above call to `Navigate()`
    // and you'll end up in a loop.

    ((WebBrowser)sender).Dispose();

    // Or you could unsubscribe to the event if you still need the browser instance
    WebBrowser1.DocumentCompleted -= WebBrowser1_DocumentCompleted;
}