Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中的操作之间暂停#_C#_Vb.net - Fatal编程技术网

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

C# 如何在C中的操作之间暂停#,c#,vb.net,C#,Vb.net,在这个例子中,我使用1按钮和web浏览器在VB中使用C# 我只想按一下按钮,然后让它转到bing,等2秒钟,然后转到google。我在尝试时看到的每种方法都会在开始时暂停,而不是在导航之间。这是我的。提前谢谢 public void button1_Click(object sender, EventArgs e) { WebBrowser1.Navigate("http://www.bing.com"); Thread.sleep(2000);

在这个例子中,我使用1按钮和web浏览器在VB中使用C#

我只想按一下按钮,然后让它转到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");

    }
订阅活动并导航到第二页:

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;
}

你想要达到的最终目标是什么?为什么是2秒?您可以查看订阅
DocumentCompleted
处理程序,一旦bing页面完成加载,然后导航到google one?问题是
navigate
是异步的,因此页面仍在加载时延迟已经结束。分配一个
DocumentCompleted
事件处理程序,在那里延迟,然后导航到Google。谢谢!我还没有看过完整的文档。只花了2秒钟就让它满载。如果您有一个在这种情况下使用它的非常快速的例子,将不胜感激@OmegaPoint这很简单,但我会为你添加一个答案。你可能想检查DocumentCompletedEvent中加载了哪个文档,否则它会不断加载google页面,这比我自己处理webbrowser要好得多。这真的帮了我很大的忙。谢谢大家!