public async Task <BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken token = default)
        {
            using (var form = _formFactory.Invoke())
                using (var browser = new ExtendedWebBrowser()
                {
                    Dock = DockStyle.Fill
                })
                {
                    var signal = new SemaphoreSlim(0, 1);

                    var result = new BrowserResult
                    {
                        ResultType = BrowserResultType.UserCancel
                    };

                    form.FormClosed += (o, e) =>
                    {
                        signal.Release();
                    };

                    browser.NavigateError += (o, e) =>
                    {
                        if (e.Url.StartsWith(options.EndUrl))
                        {
                            e.Cancel          = true;
                            result.ResultType = BrowserResultType.Success;
                            result.Response   = e.Url;
                            signal.Release();
                        }
                    };

                    browser.DocumentCompleted += (o, e) =>
                    {
                        if (e.Url.AbsoluteUri.StartsWith(options.EndUrl))
                        {
                            result.ResultType = BrowserResultType.Success;
                            result.Response   = e.Url.AbsoluteUri;
                            signal.Release();
                        }
                    };

                    try
                    {
                        form.Controls.Add(browser);
                        browser.Show();

                        form.Show();
                        browser.Navigate(options.StartUrl);

                        await signal.WaitAsync();
                    }
                    finally
                    {
                        form.Hide();
                        browser.Hide();
                    }

                    return(result);
                }
        }
        public async Task <BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken token = default)
        {
            using (var form = _formFactory.Invoke())
                using (var browser = new ExtendedWebBrowser()
                {
                    Dock = DockStyle.Fill
                })
                {
                    var signal = new SemaphoreSlim(0, 1);

                    var result = new BrowserResult
                    {
                        ResultType = BrowserResultType.UserCancel
                    };

                    form.FormClosed += (o, e) =>
                    {
                        signal.Release();
                    };

                    browser.NavigateError += (o, e) =>
                    {
                        e.Cancel = true;

                        if (e.Url.StartsWith(options.EndUrl))
                        {
                            result.ResultType = BrowserResultType.Success;
                            result.Response   = e.Url;
                        }
                        else
                        {
                            result.ResultType = BrowserResultType.HttpError;
                            result.Error      = e.StatusCode.ToString();
                        }

                        signal.Release();
                    };

                    browser.BeforeNavigate2 += (o, e) =>
                    {
                        if (e.Url.StartsWith(options.EndUrl))
                        {
                            e.Cancel = true;

                            result.ResultType = BrowserResultType.Success;
                            result.Response   = e.Url;
                            signal.Release();
                        }
                    };

                    form.Controls.Add(browser);
                    browser.Show();

                    System.Threading.Timer timer = null;

                    form.Show();
                    browser.Navigate(options.StartUrl);

                    await signal.WaitAsync();

                    if (timer != null)
                    {
                        timer.Change(Timeout.Infinite, Timeout.Infinite);
                    }

                    form.Hide();
                    browser.Hide();

                    return(result);
                }
        }
Ejemplo n.º 3
0
 public ExtendedWebBrowserEventHelper(ExtendedWebBrowser parent)
 {
     this.parent = parent;
 }