代码逻辑比较简单的。System.Threading.TimerCallback timerCB = new TimerCallback(ThreadDeal.DealData);
System.Threading.Timer timer = new System.Threading.Timer(timerCB, sptable, 1000, 2000);
应该是每隔2秒都要执行ThreadDeal.DealData这个方法啊。我这个方法就是简单的写日志,写上时间。我的程序是在Windows程序中,在form1的load方法中执行的。运行几秒或者20秒左右就停止了,

解决方案 »

  1.   

    如果可以,你把form1的load方法贴出来。
      

  2.   

    我这样不会停止
            private void Form1_Load(object sender, EventArgs e)
            {            System.Threading.TimerCallback timerCB = new System.Threading.TimerCallback(TimerCallbackFunc);
                System.Threading.Timer timer = new System.Threading.Timer(timerCB, null, 1000, 2000); 
            }        private void TimerCallbackFunc(object state)
            {
                Console.WriteLine(DateTime.Now.ToString());
            }
      

  3.   

    这是原来的代码。原来是要根据有几个表就生成几个timer线程实例。
    private void Form1_Load(object sender, EventArgs e)
            {
                DataSet ds = new DataSet();
                ds.ReadXml(@"../../SPTable.xml");            System.Threading.Timer[] timer = new System.Threading.Timer[ds.tables[0].count];             for (int n = 0; n < ds.Tables[0].Rows.Count; n++)
                {
                    DB.SPTable sptable = new SpSubmitApp.DB.SPTable();
                    sptable.SP = ds.Tables[0].Rows[n][0].ToString();
                    sptable.Table = ds.Tables[0].Rows[n][1].ToString();
                   System.Threading.TimerCallback timerCB = new TimerCallback(ThreadDeal.DealData);
                   timer[n] = new System.Threading.Timer(timerCB, sptable, 1000, 2000);
                }   
    }
      

  4.   

    我看网上文章说,这个System.threading.timer在Windows程序下比较特殊,
    他的对象必须定义在方法体的外面。
    于是我定义一个System.threading.timer timer =null在load方法外面,而且我这次只使用了一个timer,没有使用数组
    发现这些运行就正常了,日志写的正常。这是咋回事
    给大家看看这个文章
    http://topic.csdn.net/t/20060603/19/4798527.html
      

  5.   


    private void Form1_Load(object sender, EventArgs e)
    {
        //...
        System.Threading.Timer[] timer = new System.Threading.Timer[ds.tables[0].count];    for ()
        {
            //...
            timer[n] = new System.Threading.Timer(timerCB, sptable, 1000, 2000);
        }
    }
    你的Timer没有被其他地方引用。当运行一阵子,开始垃圾回收的时候,timer被回收,
    timer中的各项被Dispose,你的timerCB也就停止被回调了。
      

  6.   

    解决的方法就是类似你在8楼贴的代码,保持一个对timer的引用。
      

  7.   


      
                System.Threading.Timer myTimer = new System.Threading.Timer(AutoStatGPSData);
                myTimer.Change(b, autoRunIva);//代码在Form_load中,运行正常....
    System.Threading.Timer它的确和别的变量不太一样,它是另开了线程.. 线程在一般情况下是不会自动销毁的,你的问题应该不是因为timer是否全局变量引起的.. msdn上说:为 callback 指定的方法应是可重入的,这是因为 ThreadPool 线程会调用该方法。该方法在以下两种情况下可以同时在两个线程池线程中执行:一是计时器间隔小于执行该方法所需的时间;二是所有线程池线程都在使用,并且多次对该方法进行排队。  与线程池有关.. 鄙人如果没记错的话 线程池全局25个.用完的话其它全部阻塞.. 而timer是否新开的thread还是它往常那样的异步调用就不知道了... 如果是异步调用参照我在4楼说的..  
     鄙人愚见!!
      

  8.   

    9楼,我即使System.Threading.Timer[] timer = new System.Threading.Timer[ds.tables[0].count];
    把这个代码放在方法体外面,他也是执行一会就停止了。
    异步回调那个方法难道也要放在方法体外面吗??
      

  9.   

    哈哈。我终于搞定了
    我一直搞web编程。刚接触Windows发现还真有意思啊。System.Threading.Timer[] timer = new System.Threading.Timer[ds.tables[0].count]; 
    这个东西,真的要定义在方法体外,全局变量。
    还有那个回调方法最好也要放在方法体外,全局变量原因吗可能就是那个垃圾回收的机制问题。
      

  10.   


    不需要,timer保持了对回调的引用。
    class Form1 : From
    {
       System.Threading.Timer[] timers;    private void Form1_Load(...)
       {
       }
    }这样写,除非From1的实例被回收了,timer都不会被回收。
      

  11.   

    dll信息:
    namespace System.Threading
    {
        // 摘要:
        //     提供以指定的时间间隔执行方法的机制。无法继承此类。
        [ComVisible(true)]
        public sealed class Timer : MarshalByRefObject, IDisposable
        {
            ...
            // 摘要:
            //     更改计时器的启动时间和方法调用之间的间隔,用 32 位有符号整数度量时间间隔。
             //
            // 参数:
            //   dueTime:
            //     在调用构造 System.Threading.Timer 时指定的回调方法之前的延迟时间量(以毫秒为单位)。指定 System.Threading.Timeout.Infinite
            //     可防止重新启动计时器。指定零 (0) 可立即重新启动计时器。
             //
            //   period:
            //     调用构造 System.Threading.Timer 时指定的回调方法的时间间隔(以毫秒为单位)。指定 System.Threading.Timeout.Infinite
            //     可以禁用定期终止。
             //
            // 返回结果:
            //     如果尚未释放当前实例,则为 true;否则为 false。
             //
            // 异常:
            //   System.ObjectDisposedException:
            //     System.Threading.Timer 已被释放。
             //
            //   System.ArgumentOutOfRangeException:
            //     dueTime 或 period 参数为负,并且不等于 System.Threading.Timeout.Infinite。
             public bool Change(int dueTime, int period);
            ...
        }
    }由此可见:
    threadTime = new System.Threading.Timer(new TimerCallback(GetXmlDocument), "cs,sh", 100, 1000);
    实例化之后,需要停止可以使用
    threadTime.Change(0, System.Threading.Timeout.Infinite);