本帖最后由 ANARYI 于 2014-11-04 16:44:46 编辑

解决方案 »

  1.   

    5秒   new一个Thread  不卡死才怪。
      

  2.   

    可以使用Dispatcher ,而不是使用thread
      

  3.   

    MSDN上有基础讲解,给搜了一下。
    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
      

  4.   

    其实我一直没弄懂,Dispatcher的用法。
    例如下面的例子,如果我一次性操作了大量的控件有应该怎么写呢?       this.txtNote.Dispatcher.Invoke(new Action(() =>
            {
                this.txtNote.Text += notes;
                this.txtNote.Text += "\r";
                this.txtNote.ScrollToEnd();
            }));
      

  5.   

    其实我一直没弄懂,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来操作控件。
      

  6.   

    我现在把代码修改成这个样子了。     _dispatcher.Invoke(new Action(() =>
            {
                timer4Check();
            }));可是在抛出异常的时候却卡死了好几秒,请问应该怎样修改呢?
      

  7.   

    抛出异常,你应该在 timer4Check(); 中 把timer给停止了吧??只是猜测
      

  8.   

    抛出异常,你应该在 timer4Check(); 中 把timer给停止了吧??只是猜测
    ???不是很明白,为什么timer给停止了会使界面卡死呢?
      

  9.   

    抛出异常,你应该在 timer4Check(); 中 把timer给停止了吧??只是猜测
    ???不是很明白,为什么timer给停止了会使界面卡死呢?
    都抛出异常 ,你的timer 还在运行,你不觉得不合理吗?
      

  10.   

    抛出异常,你应该在 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();
                    }
                }));
            }
      

  11.   

     _dispatcher.BeginInvoke-》timer4.Dispatcher.BeginInvoke
      

  12.   

    小白看得不是很明白,请问能够解释一下吗
    还卡死么??
    卡啊,请问_dispatcher.BeginInvoke-》timer4.Dispatcher.BeginInvoke是什么意思啊?
      

  13.   

    “断网了”就别重复了。发现可用性方面bug是好事,它告诉你下一步,你应该动点脑筋去重新设计程序逻辑流程。不要问“有什么技术”,先把你的流程设计好。
      

  14.   

    dispatcher你可以理解为就是一个操作handler入列和出列的处理过程楼主的代码既然涉及到网络连接数据读取,那么应该让自己代码更严谨点,自己处理网络连接异常的问题,而不是将这样的问题交给异常处理机制。至于实现的方式,我觉得楼上的大多数都提到了