public Bitmap CaptureHtml(int timeoutMs) { if (timeoutMs <= 0) { throw new ArgumentException("You must specify a timeout value greater than 0", "timeoutMs"); } // set the timeout _timeoutMs = timeoutMs; ConditionVariable signal = new ConditionVariable(); // open a new form on a background, STA thread Thread formThread = ThreadHelper.NewThread(new ThreadStart(delegate { ThreadMain(signal, _ids); }), "BrowserScreenCaptureForm", true, true, true); formThread.Start(); // wait for it to complete or timeout using (WaitCursor waitCursor = ShowWaitCursor ? new WaitCursor() : null) { signal.WaitForSignal(30000); // this should actually return very quickly if (formThread.Join(_timeoutMs)) { // throw an exception if one occurred if (_exception != null) { throw _exception; } // return the captured bitmap return((Bitmap)Bitmap.FromStream(StreamHelper.AsStream(_capturedBitmap))); } else { // timed out, make sure we tell the form to close if // it hasn't already _applicationContext.CloseFormAndExit(); // return null return(null); } } }
public Bitmap CaptureHtml(int timeoutMs) { if (timeoutMs <= 0) throw new ArgumentException("You must specify a timeout value greater than 0", "timeoutMs"); // set the timeout _timeoutMs = timeoutMs; ConditionVariable signal = new ConditionVariable(); // open a new form on a background, STA thread Thread formThread = ThreadHelper.NewThread(new ThreadStart(delegate { ThreadMain(signal, _ids); }), "BrowserScreenCaptureForm", true, true, true); formThread.Start(); // wait for it to complete or timeout using (WaitCursor waitCursor = ShowWaitCursor ? new WaitCursor() : null) { signal.WaitForSignal(30000); // this should actually return very quickly if (formThread.Join(_timeoutMs)) { // throw an exception if one occurred if (_exception != null) throw _exception; // return the captured bitmap return (Bitmap)Bitmap.FromStream(StreamHelper.AsStream(_capturedBitmap)); } else { // timed out, make sure we tell the form to close if // it hasn't already _applicationContext.CloseFormAndExit(); // return null return null; } } }
private void ThreadMain(ConditionVariable signal, string[] ids) { HtmlScreenCaptureForm form = null; try { // housekeeping initialization Application.OleRequired(); // create the form and execute the capture form = new HtmlScreenCaptureForm(this); this.Ids = ids; form.Ids = ids; form.DoCapture(); // Create and run the form _applicationContext = new FormLifetimeApplicationContext(form); signal.Signal(); Application.Run(_applicationContext); // propragate exceptions that happened inside the AppContext if (_applicationContext.Exception != null) { throw _applicationContext.Exception; } } catch (Exception ex) { _exception = ex; } finally { if (form != null) { form.Close(); } } }
private void ThreadMain(ConditionVariable signal, string[] ids) { HtmlScreenCaptureForm form = null; try { // housekeeping initialization Application.OleRequired(); // create the form and execute the capture form = new HtmlScreenCaptureForm(this); this.Ids = ids; form.Ids = ids; form.DoCapture(); // Create and run the form _applicationContext = new FormLifetimeApplicationContext(form); signal.Signal(); Application.Run(_applicationContext); // propragate exceptions that happened inside the AppContext if (_applicationContext.Exception != null) throw _applicationContext.Exception; } catch (Exception ex) { _exception = ex; } finally { if (form != null) form.Close(); } }