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

发布于 2024-11-19 10:40:10 字数 209 浏览 4 评论 0原文

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

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

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

I have a for loop and inside there is a navigate method for a browser. and it's suppose to load diffrent sites, but the problem is that it will start to load 1 site and before it will load it, it'll load another site. so I need to like pause it until it's completed.

I started to write an event to when the ProgressChanged event is at 100%.. than I figured I don't have any idea what to do next but I think it's a start.
Please help, Thanks!

Edit: I am using Forms as Roland said.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软的没边 2024-11-26 10:40:10

我假设您正在进行 Windows 窗体编程。您想要的事件是 DocumentCompleted 这里是一个例子:

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

I assume you are doing windows forms programming. The event you want is DocumentCompleted Here's an example:

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

For a list of URIs it's straight forward.

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];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文