Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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#_Events_Loops_For Loop_Load - Fatal编程技术网

C#:停止循环直到发生事件

C#:停止循环直到发生事件,c#,events,loops,for-loop,load,C#,Events,Loops,For Loop,Load,我有一个用于循环的,里面有一个用于浏览器的导航方法。它假设加载不同的站点,但问题是它将开始加载一个站点,在加载之前,它将加载另一个站点。所以我需要暂停它直到它完成 当ProgressChanged事件为100%时,我开始编写一个事件。。我想我不知道下一步该做什么,但我认为这是一个开始。 请帮忙,谢谢 编辑:我正在使用罗兰所说的表单。我假设您正在进行windows表单编程。您想要的事件如下所示: public Uri MyURI { get; set; } public Form1() {

我有一个用于循环的
,里面有一个用于浏览器的导航方法。它假设加载不同的站点,但问题是它将开始加载一个站点,在加载之前,它将加载另一个站点。所以我需要暂停它直到它完成

当ProgressChanged事件为100%时,我开始编写一个事件。。我想我不知道下一步该做什么,但我认为这是一个开始。 请帮忙,谢谢


编辑:我正在使用罗兰所说的表单。

我假设您正在进行windows表单编程。您想要的事件如下所示:

public Uri MyURI { get; set; }

public Form1()
{
    InitializeComponent();

    MyURI = new Uri("http://stackoverflow.com");
    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
    webBrowser1.Url = MyURI;
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if(e.Url == MyURI)
        MessageBox.Show("Page Loaded");
}
对于URI列表,这是直截了当的

public int CurrentIndex = 0;
List<Uri> Uris;

public Form1()
{
    InitializeComponent();

    Uris = new List<Uri> { new Uri("http://stackoverflow.com"), new Uri("http://google.com/") };

    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

    webBrowser1.Url = Uris[CurrentIndex];
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = (WebBrowser)sender;
    if (e.Url == Uris[CurrentIndex])
    {
        CurrentIndex++;
        if (CurrentIndex < Uris.Count)
        {
            browser.Url = Uris[CurrentIndex];
        }
    }
}
public int CurrentIndex=0;
列出URI;
公共表格1()
{
初始化组件();
Uri=新列表{新Uri(“http://stackoverflow.com“”,新Uri(“”)http://google.com/") };
webBrowser1.DocumentCompleted+=新的WebBrowserDocumentCompletedEventHandler(webBrowser1\u DocumentCompleted);
webBrowser1.Url=Uris[CurrentIndex];
}
作废webBrowser1\u DocumentCompleted(对象发送者,WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser浏览器=(WebBrowser)发送方;
if(e.Url==Uris[CurrentIndex])
{
CurrentIndex++;
if(CurrentIndex
谢谢@Roland,但我需要有关环路的帮助。我需要让它等到文档完成,然后再重复它self@Roland谢谢,但是我用这个有问题。我在一个类中编写这个函数,我尝试使用e.Url,但它是只读的,我不太确定如何编写一个新的Url@Mathieu这将浪费一些时间,我正试图避免这种情况。@Ken,您不应该设置e.Url变量。我之所以将其包含在中,只是因为这样可以避免加载单个网页时出现太多回调,例如iframe。如果您想要对webBrowser1的引用,而您没有直接访问权限,则可以通过强制转换发件人来获得该引用:WebBrowser webBrowser1=(WebBrowser)发件人;函数定义的右下方:webBrowser1_DocumentCompleted@Roland很抱歉,新手的问题,但如果是webBrowser,我如何才能投出发件人?在函数内部,它在参数内部(我不能在这个
\u DocumentCompleted
函数中这样做)@Roland非常感谢!:)