public Browser(bool visibility) { _signal = new ManualResetEvent(false); var thread = new Thread( () => { _browser = new WebBrowser { ScriptErrorsSuppressed = true }; _browser.Navigating += (p, q) => _lastCompleted = null; _browser.DocumentCompleted += (p, q) => _lastCompleted = DateTime.UtcNow; _form = new HeadlessForm(); _form.Controls.Add(_browser); _form.HandleCreated += (p, q) => _signal.Set(); _form.InitialVisibility = visibility; _form.Visible = visibility; Application.Run(_form); } ); thread.SetApartmentState(ApartmentState.STA); thread.IsBackground = true; thread.Start(); _signal.WaitOne(); }
/// <summary> /// Initializes a new instance of the <see cref="WebBrowserWaiter"/> class. /// </summary> /// <param name="visibility"> /// The visibility. /// </param> /// <param name="position"> /// The position. /// </param> /// <param name="width"> /// The width. /// </param> /// <param name="height"> /// The height. /// </param> /// <param name="top"> /// The top. /// </param> /// <param name="left"> /// The left. /// </param> public WebBrowserWaiter(bool visibility = false, FormStartPosition position = FormStartPosition.CenterScreen, int width = -1, int height = -1, int top = 0, int left = 0) { this.signal = new ManualResetEvent(false); var thread = new Thread( () => { this.browser = new WebBrowser { Width = width < 0 ? Screen.PrimaryScreen.WorkingArea.Width * 3 / 4 : width, Height = height < 0 ? Screen.PrimaryScreen.WorkingArea.Height * 3 / 4 : height }; this.browser.Navigating += (p, q) => this.lastCompleted = null; this.browser.DocumentCompleted += (p, q) => this.lastCompleted = DateTime.UtcNow; this.form = new HeadlessForm { Width = this.browser.Width, Height = this.browser.Height, StartPosition = position, Top = top, Left = left }; this.form.Controls.Add(this.browser); this.form.HandleCreated += (p, q) => this.signal.Set(); this.form.InitialVisibility = visibility; this.form.Visible = visibility; Application.Run(this.form); } ); thread.SetApartmentState(ApartmentState.STA); thread.IsBackground = true; thread.Start(); this.signal.WaitOne(); }