发生了异常“Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException”?

Nick_Ngai 2014-07-21 09:27:28
如题,并不会进入到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)
{ }
}
...全文
1249 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Nick_Ngai 2014-07-21
  • 打赏
  • 举报
回复
引用 2 楼 findcaiyzh 的回复:
线程里不能更新UI,得用委托 参考: How to update the GUI from another thread in C#? http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c
那个方法好像不行的,导致我的程序不能循环操作,只执行一次就停掉了
引用 3 楼 findcaiyzh 的回复:
你在Timer里开了个线程,专门用于更新UI?有点奇怪,直接更新不行吗?
要求开个线程。另外要求每隔4小时调用一次程序。
宝_爸 2014-07-21
  • 打赏
  • 举报
回复
你在Timer里开了个线程,专门用于更新UI?有点奇怪,直接更新不行吗?
宝_爸 2014-07-21
  • 打赏
  • 举报
回复
线程里不能更新UI,得用委托 参考: How to update the GUI from another thread in C#? http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c
Nick_Ngai 2014-07-21
  • 打赏
  • 举报
回复
this.BeginInvoke(new System.Threading.ThreadStart(delegate() { lblCount.Text = count.ToString(); })); 调试到这里看到的错误提示:不能进入 lblCount.Text = count.ToString();
Nick_Ngai 2014-07-21
  • 打赏
  • 举报
回复
已经解决了,原因是定时导致界面卡死。 将定时调用放到线程中就好了,修改代码如下: private void btnIPCrawl_Click(object sender, EventArgs e) { Thread thread = new Thread(TimerCrawlIP); thread.IsBackground = true; thread.Start(); } /// <summary> /// 定时抓取IP /// </summary> private void TimerCrawlIP() { //隔段时间自动调用程序 System.Threading.Timer threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(InvokeIPCrawl), null, 0, 14400000); while (true) { Thread.Sleep(14400000); } }
内容概要:本文针对网型T型三电平逆变器的低电压穿越(LVRT)能力及其综合控制策略开展深入研究,重点分析电网电压跌落条件下逆变器的动态响应特性与稳定性控制方法。研究基于Simulink平台构建详细的系统仿真模型,融合改进的电流环控制策略与中点电位平衡控制算法,有效提升逆变器在故障工况下的电流跟踪精度、过流抑制能力和直流侧中点电压稳定性。通过多场景仿真验证,所提出的综合控制策略能够显著增强逆变器在低电压穿越过程中的鲁棒性与电能质量,确保新能源系统在电网扰动下的持续可靠并网运行。; 适合人群:电力电子、新能源发电、电力系统自动化等相关专业的研究生、科研人员及从事并网逆变器设计与控制的工程技术人员。; 使用场景及目标:①深入理解三电平逆变器在电网故障下的运行机理与控制挑战;②掌握低电压穿越技术中电流限幅、故障响应及中点电位平衡的核心控制方法;③应用于光伏、风电等新能源并网系统的控制策略开发与仿真验证; 阅读建议:建议结合提供的Simulink仿真模型进行动手实践,重点关注控制器参数设计、中点电位观测与调节模块的实现逻辑,并通过对比不同电压跌落深度和控制策略的仿真结果,深化对系统动态行为与控制性能的理解。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧