为什么我的子线程循环发送数据会死掉?
代码如下
        private void  AutoSend()
        {
            while (true)
            {
                Thread.Sleep(1000);
                
                if (null == pAutoBuff)
                {
                    return;
                }
                try
                {
                    if (!Send(pAutoBuff, 10000))//send是自己写的函数,pAutoBuff是数据,1000是发送时间
                    {
                        return;
                    }
                }
                catch (System.Exception)
                {
                    return;
                }            }
求高人指点
         

解决方案 »

  1.   

    把你线程调用AutoSend()贴出看下
      

  2.   

    搞不清楚lz线程是执行AutoSend(用线程执行这个方法感觉就不好).还是你的发包方法,
    可以试下把发包的方法委托处理,
    睡觉先~
      

  3.   

    ...csdn升级...
    模似下,lz的发包交给后台线程去做就好了.以下方式效果一样.不过没做死包处理:        static DateTime sendTime = DateTime.Now;//模似发包对象
            static AutoResetEvent threadEvent = new AutoResetEvent(false);
            void test()
            {
                System.Timers.Timer t = new System.Timers.Timer();
                t.Interval = 1000;
                t.Elapsed += (a, b) =>
                {
                    threadEvent.Reset();
                    Thread td = new Thread(() =>
                    {
                        sendTime = DateTime.Now;
                        if(sendTime != null)
                        Console.WriteLine("模似发包方法:{0}", sendTime.ToString());
                        Thread.Sleep(1000);//发包使用1S,不管成功与否都释放
                        threadEvent.Set();
                    });
                    td.IsBackground = true;
                    td.Start();
                };
                t.Start();
            }
      

  4.   


    ........我比你菜多了,相信自己,困难会有的,但总会解决的.慢慢来,大男人一个!
    那你试下你原方法中:
    this.autosend把此线程设置为后台线程this.autosend.IsBackground= true;
    或是直接使用线程池去做.自己慢慢google下,少玩,这时代玩不起.你行的!
      

  5.   


    谢谢你,我终于晓得为什么要卡了,是因为我用了一个timer来打印收到的数据,我现在改用控件的.invoke来实现打印,这样发送的数据不会产生延时,但是打印的数据很多(因为每一秒钟都要打印一次),结果界面上还是有点卡,
    对于这个问题我又在每次打印之前把原来打印出来的数据,给clear了,就不卡了,我想问问为什么会出现这个情况?
      

  6.   

    http://www.cnblogs.com/yuyijq/archive/2010/01/11/1643802.html 这里说得很明白了,你可以看下