private void DownloadWebpage() { string URL = TextBox1.Text; AutoResetEvent resultEvent = new AutoResetEvent(false); string result = null; bool visible = this.Checkbox1.Checked; IEBrowser browser = new IEBrowser(visible, URL, resultEvent); // wait for the third thread getting result and setting result event EventWaitHandle.WaitAll(new AutoResetEvent[] { resultEvent }); // the result is ready later than the result event setting sometimes while (browser == null || browser.HtmlResult == null) Thread.Sleep(5); result = browser.HtmlResult; if (!visible) browser.Dispose(); //把获取的html内容通过label显示出来 Label2.Text = result; //保存结果到本程序的目录中 string path = Request.PhysicalApplicationPath; TextWriter tw = new StreamWriter(path + @"softlab/result.html"); tw.Write(result); tw.Close(); //open a new web page to display result got from webbrowser. Response.Output.WriteLine("<script>window.open ('result.html','mywindow','location=1,status=0,scrollbars=1,resizable=1,width=600,height=600');</script>"); }
// this is in the main thread protected void Page_Load(object sender, EventArgs e) { try { AutoResetEvent resultEvent = new AutoResetEvent(false); string result = null; bool visible = true; browser = new IEBrowser(visible, resultEvent); // wait for the third thread getting result and setting result event EventWaitHandle.WaitAll(new AutoResetEvent[] { resultEvent }); // the result is ready later than the result event setting somtimes while (browser == null || browser.HtmlResult == null) { Thread.Sleep(5); } result = browser.HtmlResult; if (!visible) { browser.Dispose(); } string path = Request.PhysicalApplicationPath; TextWriter tw = new StreamWriter(path + "result.html"); tw.Write(result); tw.Close(); Response.Output.WriteLine("<script>window.open ('result.html','mywindow','location=1,status=0,scrollbars=1,resizable=1,width=600,height=600');</script>"); } catch (Exception ex) { } }