发生了异常“Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException”?
如题,并不会进入到catch,但是没有更新UI,不知道什么原因。
我的代码如下:
private void btnIPCrawl_Click(object sender, EventArgs e)
{
//隔段时间自动调用程序
System.Threading.Timer threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(InvokeIPCrawl), null, 0, 14400000);
while (true)
{
Thread.Sleep(14400000);
}
}
//分别调用抓取IP方法
private void InvokeIPCrawl(Object state)
{
SaveIPToDB(proxy360, "span");
}
/// <summary>
/// 将网络抓取的IP地址保存的数据库
/// </summary>
/// <param name="url">URL地址</param>
/// <param name="tagname">标签名称(不可为空)</param>
protected void SaveIPToDB(string url, string tagname)
{
try
{
string html = CUtils.GetHtmlSource(url);
Regex reg = CUtils.GetRegexIP(tagname);
if (reg != null)
{
CDatabase db = new CDatabase("phone");
try
{
MatchCollection mc = reg.Matches(html);
foreach (Match m in mc)
{
//抓取字符串中IP地址
MatchCollection submc = Regex.Matches(m.Groups[0].ToString(), @"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}");
foreach (Match item in submc)
{
if (CUtils.isIP(item.Groups[0].ToString()))
{
//插入数据库操作
string remark = "自动抓取的代理IP@" + url;
CDatabaseDAL.p_badagentip_insert(item.Groups[0].ToString(), remark, db);
count++;
this.BeginInvoke(new System.Threading.ThreadStart(delegate()
{
lblCount.Text = count.ToString();
}));
}
}
}
}
catch (Exception ex)
{ }
finally
{
db.close();
}
}
}
catch (Exception ex)
{ }
}