.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)
            {
                Thread.Sleep(2000);
                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;
                }
            }
        }

最新源码下载

点击下载C#V1.1源码

 

历史源码下载

点击下载C#V1.0源码

 

更新日志:

2014-8-13

1.修复网站加载完毕有时候判断失败问题。

2.修复cpu占用很高问题。

 

posted @ 2014-08-13 12:41  事理  阅读(3096)  评论(10编辑  收藏  举报