首页 > 代码库 > .NET网站自动浏览器分享,解决IIS6应用池回收后第一次访问慢问题
.NET网站自动浏览器分享,解决IIS6应用池回收后第一次访问慢问题
.NET开发的网站,如果不是使用预编译发布,网站会在iis6应用池回收后第一次访问很慢,为了解决这个问题,今天写了一个自动浏览的工具,现在分享给大家,界面如下。
关键部分源码
//手动点击浏览 private void btnBrowsing_Click(object sender, EventArgs e) { if (btnBrowsing.Enabled == true && chkEnableAutomaticBrowsing.Checked == true) { btnBrowsing.Enabled = false; Thread thread = new Thread(new ThreadStart(AutoBrowsing)); thread.IsBackground = true; thread.Start(); } else { MessageBox.Show("任务正在执行!"); } } DateTime tempTime = DateTime.MinValue;//防止一分钟内刷新多次 private void timer1_Tick(object sender, EventArgs e) { DateTime dt = DateTime.Now; //转钟时清除一天日志 if (dt.ToLongTimeString() == "0:00:00") { rtxtLogs.Text = string.Empty; } TimeSpan ts = dt - tempTime; //1分钟内不得刷新两次 if (ts.TotalMinutes > 0) { //到点自动刷新 if (lstTime.Items.Contains(dt.ToShortTimeString())) { tempTime = dt.AddMinutes(1); btnBrowsing_Click(null, null); } } } private void AutoBrowsing() { int itemTotalCount = 0; CrossThreadCalls.SafeInvoke(this.lvWebsitInfo, new CrossThreadCalls.TaskDelegate(delegate() { itemTotalCount = lvWebsitInfo.Items.Count; })); for (int i = 0; i < itemTotalCount; i++) { string url = string.Empty; CrossThreadCalls.SafeInvoke(this.lvWebsitInfo, new CrossThreadCalls.TaskDelegate(delegate() { url = lvWebsitInfo.Items[i].SubItems[2].Text; })); int retry = 0; retryFlag: CrossThreadCalls.SafeInvoke(this.webBrowser1, new CrossThreadCalls.TaskDelegate(delegate() { webBrowser1.Navigate(url); })); if (WaitWebPageLoad()) { CrossThreadCalls.SafeInvoke(this.rtxtLogs, new CrossThreadCalls.TaskDelegate(delegate() { rtxtLogs.AppendText(url + "浏览成功,浏览时间:" + DateTime.Now.ToLongTimeString() + "\r\n\r\n"); rtxtLogs.ScrollToCaret(); })); CrossThreadCalls.SafeInvoke(this.lvWebsitInfo, new CrossThreadCalls.TaskDelegate(delegate() { lvWebsitInfo.Items[i].SubItems[3].Text = DateTime.Now.ToString(); })); } else { CrossThreadCalls.SafeInvoke(this.rtxtLogs, new CrossThreadCalls.TaskDelegate(delegate() { rtxtLogs.AppendText(url + "浏览失败,即将重试" + (retry + 1) + "次\r\n"); rtxtLogs.ScrollToCaret(); })); if (retry < 2)//重试3次 { retry++; Thread.Sleep(3000); goto retryFlag; } CrossThreadCalls.SafeInvoke(this.rtxtLogs, new CrossThreadCalls.TaskDelegate(delegate() { rtxtLogs.AppendText(url + "浏览失败\r\n\r\n"); rtxtLogs.ScrollToCaret(); })); CrossThreadCalls.SafeInvoke(this.lvWebsitInfo, new CrossThreadCalls.TaskDelegate(delegate() { lvWebsitInfo.Items[i].SubItems[3].Text = "自动浏览失败"; })); } } CrossThreadCalls.SafeInvoke(this.btnBrowsing, new CrossThreadCalls.TaskDelegate(delegate() { btnBrowsing.Enabled = true; })); CrossThreadCalls.SafeInvoke(this.webBrowser1, new CrossThreadCalls.TaskDelegate(delegate() { webBrowser1.Navigate("about:blank"); })); } /// <summary> /// 判断webbrowser是否加载一个网页完毕,可判断包括iframe的网页 /// </summary> /// <returns></returns> private bool WaitWebPageLoad() { int i = 0; string sUrl = string.Empty; while (true) { Delay(50); //系统延迟50毫秒,够少了吧! WebBrowserReadyState webBrowserState = WebBrowserReadyState.Uninitialized; CrossThreadCalls.SafeInvoke(this.webBrowser1, new CrossThreadCalls.TaskDelegate(delegate() { webBrowserState = webBrowser1.ReadyState; })); if (webBrowserState == WebBrowserReadyState.Complete) //先判断是否发生完成事件。 { bool isBusy = true; CrossThreadCalls.SafeInvoke(this.webBrowser1, new CrossThreadCalls.TaskDelegate(delegate() { isBusy = webBrowser1.IsBusy; })); if (!isBusy) //再判断是浏览器是否繁忙 { i = i + 1; if (i == 2) { CrossThreadCalls.SafeInvoke(this.webBrowser1, new CrossThreadCalls.TaskDelegate(delegate() { sUrl = webBrowser1.Url.ToString(); })); if (sUrl.Contains("res") || sUrl.Contains("daohang.114so.cn")) //这是判断没有网络的情况下 { return false; } else { return true; } } continue; } i = 0; } } } private void Delay(int Millisecond) //延迟系统时间,但系统又能同时能执行其它任务; { DateTime current = DateTime.Now; while (current.AddMilliseconds(Millisecond) > DateTime.Now) { Application.DoEvents();//转让控制权 } return; }
点击下载C#源码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。