与GeckoFX Web浏览器使用异步

时间:2014-05-18 09:18:44

标签: c# winforms web browser geckofx

我遇到了问题,我正在尝试使用特定网站的自定义网络浏览器。

我遇到GeckoFX的问题是有时我需要等待DocumentCompleted继续执行特定方法。 我不想将我的所有代码放入一个大型的DocumentCompleted事件中,因为这看起来很愚蠢和错误。

我通过如下使用Application.DoEvents()获得了代码,但我读到这不是一种正确的方法,而且webbrowser应该最好以异步方式运行。

 private void AddNewTab(string tmsAddress) //add a new browser to my form
    {
        TabPage tab = new TabPage();
        browserTabControl.TabPages.Insert(browserTabControl.TabCount - 1, tab);
        GeckoWebBrowser browser = new GeckoWebBrowser();
        tab.Controls.Add(browser);
        tmsBrowser.Dock = DockStyle.Fill;
        tmsBrowser.Navigate(address);


        tmsBrowser.DocumentCompleted += new EventHandler<Gecko.Events.GeckoDocumentCompletedEventArgs>(tmsBrowser_DocumentCompleted);
    }

页面上的导航是手动的,即用户正常使用页面,但有时他们可以使用快捷按钮到达某个地方。

private void someButton_MouseUp(object sender, MouseEventArgs e)
    {          
            GeckoWebBrowser browser = getCurrentBrowser();

            //get some data from the page here

            OpenPageInContentFrame(address + parameter1 + parameter2); //I need to wait for this page to load and then do HighlightItemRow()

            while (!eventHandled) //documentCompleted events sets the bool as 'true'
            {
                Application.DoEvents();
            }
            HighlightItemRow(browser, parameter1);
        }
    }

我想使用ManualResetEvents而不是while()和Application.DoEvents(),但是使用manResEvent.WaitOne()会导致整个应用程序冻结,包括导航,因此页面实际上从未加载。我认为这一定是因为它只是在一个线程中,我不知道如何让它工作 - 我从来没有使用任何异步等。

0 个答案:

没有答案
相关问题