一个关于在WPF里运行多线程的问题。 本帖最后由 ANARYI 于 2014-11-04 16:44:46 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 5秒 new一个Thread 不卡死才怪。 可以使用Dispatcher ,而不是使用thread MSDN上有基础讲解,给搜了一下。http://msdn.microsoft.com/zh-cn/library/system.windows.threading.dispatcher(v=vs.110).aspxhttp://msdn.microsoft.com/zh-cn/library/system.windows.threading.dispatchertimer(v=vs.110).aspx 其实我一直没弄懂,Dispatcher的用法。例如下面的例子,如果我一次性操作了大量的控件有应该怎么写呢? this.txtNote.Dispatcher.Invoke(new Action(() => { this.txtNote.Text += notes; this.txtNote.Text += "\r"; this.txtNote.ScrollToEnd(); })); 其实我一直没弄懂,Dispatcher的用法。例如下面的例子,如果我一次性操作了大量的控件有应该怎么写呢? this.txtNote.Dispatcher.Invoke(new Action(() => { this.txtNote.Text += notes; this.txtNote.Text += "\r"; this.txtNote.ScrollToEnd(); }));操作大量控件,不需要指定某个控件的dispatcher,可以指定当前的/// <summary> /// 当前线程调度器 /// </summary> private readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;可以使用_dispatcher.Invoke来操作控件。 我现在把代码修改成这个样子了。 _dispatcher.Invoke(new Action(() => { timer4Check(); }));可是在抛出异常的时候却卡死了好几秒,请问应该怎样修改呢? 抛出异常,你应该在 timer4Check(); 中 把timer给停止了吧??只是猜测 抛出异常,你应该在 timer4Check(); 中 把timer给停止了吧??只是猜测???不是很明白,为什么timer给停止了会使界面卡死呢? 抛出异常,你应该在 timer4Check(); 中 把timer给停止了吧??只是猜测???不是很明白,为什么timer给停止了会使界面卡死呢?都抛出异常 ,你的timer 还在运行,你不觉得不合理吗? 抛出异常,你应该在 timer4Check(); 中 把timer给停止了吧??只是猜测???不是很明白,为什么timer给停止了会使界面卡死呢?都抛出异常 ,你的timer 还在运行,你不觉得不合理吗?于是我参考了http://blog.csdn.net/yl2isoft/article/details/11711833我把代码改下面这样了,不抛出错误照样卡死啊。 private void timer4Check() { string sb = GetHtmlValue(Convert.ToString(ConfigurationManager.AppSettings.Get("serverCheck"))).ToLowerInvariant() ; _dispatcher.BeginInvoke(new Action(() => { if (sb == "ok") { resetTimer4(); timer2.Stop(); timer3.Stop(); } else { isDisableAll = true; LinkError(); } })); } _dispatcher.BeginInvoke-》timer4.Dispatcher.BeginInvoke 小白看得不是很明白,请问能够解释一下吗还卡死么??卡啊,请问_dispatcher.BeginInvoke-》timer4.Dispatcher.BeginInvoke是什么意思啊? “断网了”就别重复了。发现可用性方面bug是好事,它告诉你下一步,你应该动点脑筋去重新设计程序逻辑流程。不要问“有什么技术”,先把你的流程设计好。 dispatcher你可以理解为就是一个操作handler入列和出列的处理过程楼主的代码既然涉及到网络连接数据读取,那么应该让自己代码更严谨点,自己处理网络连接异常的问题,而不是将这样的问题交给异常处理机制。至于实现的方式,我觉得楼上的大多数都提到了 C# 共享类 无边框窗体拖动问题 创建多个实例 急用谢谢 子线程操作主窗口控件的问题,用什么方法? javaScript 脚本 window.org_pass = S("pp").value是什么意思 即时绑定数据问题 如何用C#监听80端口发送的url DataTable已经存在如何在里面增加2列 在web中如何编辑word文档? 【非常简单问题】电子邮件和年月日(2002/02/02)的正则表达式怎么写?谢谢 第一次写windows服务,哪里错了啊? string remove()不起作用
http://msdn.microsoft.com/zh-cn/library/system.windows.threading.dispatcher(v=vs.110).aspx
http://msdn.microsoft.com/zh-cn/library/system.windows.threading.dispatchertimer(v=vs.110).aspx
例如下面的例子,如果我一次性操作了大量的控件有应该怎么写呢? this.txtNote.Dispatcher.Invoke(new Action(() =>
{
this.txtNote.Text += notes;
this.txtNote.Text += "\r";
this.txtNote.ScrollToEnd();
}));
例如下面的例子,如果我一次性操作了大量的控件有应该怎么写呢? this.txtNote.Dispatcher.Invoke(new Action(() =>
{
this.txtNote.Text += notes;
this.txtNote.Text += "\r";
this.txtNote.ScrollToEnd();
}));
操作大量控件,不需要指定某个控件的dispatcher,可以指定当前的/// <summary>
/// 当前线程调度器
/// </summary>
private readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;可以使用_dispatcher.Invoke来操作控件。
{
timer4Check();
}));可是在抛出异常的时候却卡死了好几秒,请问应该怎样修改呢?
???不是很明白,为什么timer给停止了会使界面卡死呢?
???不是很明白,为什么timer给停止了会使界面卡死呢?
都抛出异常 ,你的timer 还在运行,你不觉得不合理吗?
???不是很明白,为什么timer给停止了会使界面卡死呢?
都抛出异常 ,你的timer 还在运行,你不觉得不合理吗?
于是我参考了http://blog.csdn.net/yl2isoft/article/details/11711833我把代码改下面这样了,不抛出错误照样卡死啊。 private void timer4Check()
{
string sb = GetHtmlValue(Convert.ToString(ConfigurationManager.AppSettings.Get("serverCheck"))).ToLowerInvariant() ;
_dispatcher.BeginInvoke(new Action(() =>
{
if (sb == "ok")
{
resetTimer4();
timer2.Stop();
timer3.Stop();
}
else
{
isDisableAll = true;
LinkError();
}
}));
}
还卡死么??
卡啊,请问_dispatcher.BeginInvoke-》timer4.Dispatcher.BeginInvoke是什么意思啊?