用System.Timers.Timer来做
我需要一个定时器来驱动程序开始与停止
是这样的有两个方法,一个方法是开始记录(A),一个方法是停止记录(B),有个布尔值来标识停止与启动C程序刚启动调用A方法,然后过20秒后停止,调用B方法,如果发现C为停止状态,然后再过10秒调用A方法,发现C状态为启动状态,再过10秒调用B方法,就这样不停的循环求代码

解决方案 »

  1.   

    潦草的写了一下,没有编译。也就大概说明个思路:    volatile bool _isRunning;
        void Init(){
                Timer checker = new Timer();
                checker.Interval = 10000;
                checker.Tick += delegate(object o, EventArgs ea)
                {
                    //这个Timer 判断状态,决定执行哪个Method。
                    if(_isRunning)
                        MethodA();
                    else
                        MethodB();
                };
                checker.Start();
                Timer cmd = new Timer();
                cmd.Interval = 10000;
                //这个Timer负责改变状态.
                cmd.Tick += delegate(object obj, EventArgs eva) { _isRunning = !isRunning; };
                cmd.Start();
        }
      

  2.   

    checker.Tick? 兄弟,我想用System.Timers.Timer这个貌似没有Tick
      

  3.   

    System.Timers.Timer
    的话就把Tick 改成注册到 Elapsed 事件就好了,事件参数object o, EventArgs ea)改成(object sender, System.Timers.ElapsedEventArgs e);
    不过这个方法如果在UI下面使用的话,相当麻烦.它不是线程同步的.动手试试呗.
      

  4.   

    我也试着编写了一个,不知道这样是不是对的。using System;
    using System.Windows.Forms;
    namespace ConsoleApplicationTimer
    {
        public class TestTimer
        {
            static Timer myTimer = new Timer();
            static bool exitFlag = false;
            static bool isRunning = true;        // This is the method to run when the timer is raised.
            private static void TimerEventProcessor(Object myObject,
                                                    EventArgs myEventArgs)
            {            if (isRunning)
                {
                    MedhodA();
                    isRunning = false;
                }
                else
                {
                    MedhodB();
                    isRunning = true;
                }
            }
            /*The MethodA()*/
            static void MedhodA()
            {
                Console.WriteLine("方法A");
            }
            /*The MethodB()*/
            static void MedhodB()
            {
                Console.WriteLine("方法B");
            }        public static void Main()
            {
                /* Adds the event and the event handler for the method that will 
                   process the timer event to the timer. */
                myTimer.Tick += new EventHandler(TimerEventProcessor);            // Sets the timer interval to 10 seconds.
                myTimer.Interval = 10000;
                myTimer.Start();            // Runs the timer, and raises the event.
                while (exitFlag == false)
                {
                    // Processes all the events in the queue.
                    Application.DoEvents();
                }
            }
        }}
      

  5.   


    using System;
    using System.Timers;public class Timer1
    {
        private static Timer aTimer;
        static bool isRunning = true;
            public static void Main()
        {
            aTimer = new System.Timers.Timer();        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 20000;
            aTimer.Enabled = true;
            Console.WriteLine("A方法");
            Console.ReadLine();    }
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            if (isRunning)
            {
                aTimer.Interval = 10000;
                Console.WriteLine("B方法");
                isRunning = false;
            }
            else
            {
                Console.WriteLine("A方法");
                isRunning = true;
            }
        }
    }
      

  6.   

    还没完成啊? MethodA,B构造如下.这种只能说提出思路,如果所要等待多少秒不执行的话,那还不如用Thread.Sleep来得方便.volatile bool isRunning = true;
            System.Timers.Timer timer1;
            System.Timers.Timer timer2;
            void Init()
            {
                timer1 = new System.Timers.Timer();
                timer2 = new System.Timers.Timer();
                timer1.Interval = 20000;
                timer2.Interval = 10000;
                timer1.Elapsed += delegate(object o, System.Timers.ElapsedEventArgs e)
                {
                    isRunning = false;
                     timer1.Stop();
                    timer2.Start();
                };
                timer2.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs ea)
                {
                    isRunning = !isRunning;
                };
                timer1.Start();
                MethodA();
            }
            void MethodA()
            {
                while (isRunning)
                {
                    //wait or do something ...
                }
                MethodB();
            }
            void MethodB()
            {
                while (!isRunning)
                {
                    //wait or do something ...
                }
                MethodA();
            }
      

  7.   

             强人、  ding 、