Exemple #1
0
        public async Task <InvokeResult> InvokeAsync(InvokeOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.StartUrl))
            {
                throw new ArgumentException("Missing StartUrl", nameof(options));
            }
            if (string.IsNullOrWhiteSpace(options.EndUrl))
            {
                throw new ArgumentException("Missing EndUrl", nameof(options));
            }

            switch (options.InitialDisplayMode)
            {
            case DisplayMode.Visible:
                return(await InvokeAsyncCore(options, false));

            case DisplayMode.Hidden:
                var result = await InvokeAsyncCore(options, true);

                if (result.ResultType != InvokeResultType.Success)
                {
                    var args = new HiddenModeFailedEventArgs(result);
                    HiddenModeFailed?.Invoke(this, args);
                    if (!args.Cancel)
                    {
                        result = await InvokeAsyncCore(options, false);
                    }
                }
                return(result);
            }

            throw new ArgumentException("Invalid DisplayMode", nameof(options));
        }
        public async Task<InvokeResult> InvokeAsync(InvokeOptions options)
        {
            using (var form = _formFactory.Invoke())
            using (var browser = new ExtendedWebBrowser()
            {
                Dock = DockStyle.Fill
            })
            {
                var signal = new SemaphoreSlim(0, 1);

                var result = new InvokeResult
                {
                    ResultType = InvokeResultType.UserCancel
                };

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

                browser.NavigateError += (o, e) =>
                {
                    e.Cancel = true;
                    result.ResultType = InvokeResultType.HttpError;
                    result.Error = e.StatusCode.ToString();
                    signal.Release();
                };

                browser.BeforeNavigate2 += (o, e) =>
                {
                    if (e.Url.StartsWith(options.EndUrl))
                    {
                        e.Cancel = true;
                        result.ResultType = InvokeResultType.Success;
                        if (options.ResponseMode == ResponseMode.FormPost)
                        {
                            result.Response = Encoding.UTF8.GetString(e.PostData ?? new byte[] { });
                        }
                        else
                        {
                            result.Response = e.Url;
                        }
                        signal.Release();
                    }
                };

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

                System.Threading.Timer timer = null;
                if (options.InitialDisplayMode != DisplayMode.Visible)
                {
                    result.ResultType = InvokeResultType.Timeout;
                    timer = new System.Threading.Timer((o) =>
                    {
                        var args = new HiddenModeFailedEventArgs(result);
                        HiddenModeFailed?.Invoke(this, args);
                        if (args.Cancel)
                        {
                            browser.Stop();
                            form.Invoke(new Action(() => form.Close()));
                        }
                        else
                        {
                            form.Invoke(new Action(() => form.Show()));
                        }
                    }, null, (int)options.InvisibleModeTimeout.TotalSeconds * 1000, Timeout.Infinite);
                }
                else
                {
                    form.Show();
                }

                browser.Navigate(options.StartUrl);

                await signal.WaitAsync();
                if (timer != null) timer.Change(Timeout.Infinite, Timeout.Infinite);

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

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

                    var result = new InvokeResult
                    {
                        ResultType = InvokeResultType.UserCancel
                    };

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

                    browser.NavigateError += (o, e) =>
                    {
                        e.Cancel          = true;
                        result.ResultType = InvokeResultType.HttpError;
                        result.Error      = e.StatusCode.ToString();
                        signal.Release();
                    };

                    browser.BeforeNavigate2 += (o, e) =>
                    {
                        if (e.Url.StartsWith(options.EndUrl))
                        {
                            e.Cancel          = true;
                            result.ResultType = InvokeResultType.Success;
                            if (options.ResponseMode == ResponseMode.FormPost)
                            {
                                result.Response = Encoding.UTF8.GetString(e.PostData ?? new byte[] { });
                            }
                            else
                            {
                                result.Response = e.Url;
                            }
                            signal.Release();
                        }
                    };

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

                    System.Threading.Timer timer = null;
                    if (options.InitialDisplayMode != DisplayMode.Visible)
                    {
                        result.ResultType = InvokeResultType.Timeout;
                        timer             = new System.Threading.Timer((o) =>
                        {
                            var args = new HiddenModeFailedEventArgs(result);
                            HiddenModeFailed?.Invoke(this, args);
                            if (args.Cancel)
                            {
                                browser.Stop();
                                form.Invoke(new Action(() => form.Close()));
                            }
                            else
                            {
                                form.Invoke(new Action(() => form.Show()));
                            }
                        }, null, (int)options.InvisibleModeTimeout.TotalSeconds * 1000, Timeout.Infinite);
                    }
                    else
                    {
                        form.Show();
                    }

                    browser.Navigate(options.StartUrl);

                    await signal.WaitAsync();

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

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

                    return(result);
                }
        }
        public async Task<InvokeResult> InvokeAsync(InvokeOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.StartUrl)) throw new ArgumentException("Missing StartUrl", nameof(options));
            if (string.IsNullOrWhiteSpace(options.EndUrl)) throw new ArgumentException("Missing EndUrl", nameof(options));

            switch (options.InitialDisplayMode)
            {
                case DisplayMode.Visible:
                    return await InvokeAsyncCore(options, false);

                case DisplayMode.Hidden:
                    var result = await InvokeAsyncCore(options, true);
                    if (result.ResultType != InvokeResultType.Success)
                    {
                        var args = new HiddenModeFailedEventArgs(result);
                        HiddenModeFailed?.Invoke(this, args);
                        if (!args.Cancel)
                        {
                            result = await InvokeAsyncCore(options, false);
                        }
                    }
                    return result;
            }

            throw new ArgumentException("Invalid DisplayMode", nameof(options));
        }