可能你的系统还有其它问题,比如,代码里有远程访问,或数据库原因导致打开速度慢。
需要针对性检查。
关键部分源码//手动点击浏览
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 = 0i <itemTotalCounti++)
{
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
}
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)