以异步方式运行Windows.Forms.webbrowser.Navigate?

时间:2017-10-13 11:08:34

标签: c# winforms asynchronous

我正在编写一个测试程序,通过在异步任务函数中运行Navigate(URL)方法,在windows.form的Web浏览器上打开一个网页(exp。http://www.microsoft.com)...

但是它没有打开表单并显示网页,虽然我在那里设置了断点并且看到了Navigate之前的函数被调用,但是没有导航函数...

表格代码:

public partial class ImmoScoutForm : Form
{
    public ImmoScoutForm()
    {
        InitializeComponent();
        //this.immoScoutWebBrowser.Navigate("http://www.microsoft.com");
        //NavigateWebPage();

        this.immoScoutWebBrowser.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(webBrowser_Navigated);
        this.immoScoutWebBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(WebBrowserDocumentCompleted);
    }

    public void NavigateWebPage()
    {
        this.immoScoutWebBrowser.Navigate("http://www.microsoft.com");
    }

    public void NavigateWebPage(WebBrowser wb)
    {
        if (wb != null)
        {
            wb.Navigate("http://www.microsoft.com");
        }
    }

    private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        HideScriptErrors(this.immoScoutWebBrowser, true);
        //if (ConnectBtn.IsEnabled)
        //{
        //    ConnectBtn.IsEnabled = false;
        //}
    }

    public void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        MessageBox.Show("Operation has completed !");
    }

    public static void HideScriptErrors(System.Windows.Forms.WebBrowser wb, bool Hide)
    {
        FieldInfo fiComWebBrowser = typeof(System.Windows.Forms.WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
        if (fiComWebBrowser == null) return;
        object objComWebBrowser = fiComWebBrowser.GetValue(wb);
        if (objComWebBrowser == null) return;
        objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
    }

}

class program.cs

static class Program
{
    private static ImmoScoutTestForms.ImmoScoutForm _immoScoutWebForm;

    public static ImmoScoutTestForms.ImmoScoutForm ImmoScoutWebForm
    {
        get { return _immoScoutWebForm; }
        set
        {
            _immoScoutWebForm = value;
        }
    }

    /// <summary>
    /// Der Haupteinstiegspunkt für die Anwendung.
    /// </summary>
    [STAThread]
    public static void Main()
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        ImmoScoutWebForm = new ImmoScoutTestForms.ImmoScoutForm();
        //OpenThePage();

        Task.Run(() => CallPageAsyn().Wait());

        //Application.Run(ImmoScoutWebForm);

    }

    public static void OpenThePage()
    {
        if(ImmoScoutWebForm != null)
        {
            ImmoScoutWebForm.immoScoutWebBrowser.Navigate("http://www.microsoft.com");
        }

        Application.Run(ImmoScoutWebForm);
    }

    public static async Task CallPageAsyn()
    {
        await TestAsyn();
        ImmoScoutWebForm.immoScoutWebBrowser.Navigate("http://www.microsoft.com");
    }


    public static async Task TestAsyn()
    {
        Console.WriteLine("Waiting: ");
    }

}

我不知道为什么“await TestAsyn();”之后的函数可以没有被调用,编译器已经完成了吗?或者你有什么想法我怎么能这样做?

0 个答案:

没有答案