0

我创建了一个 Windows 服务来运行一段代码,并在其中实现了计时器以定期运行它。

我的计时器课程是:

class TimerClass
{
    private static System.Timers.Timer aTimer;

    public static void Main()
    {

        aTimer = new System.Timers.Timer(1000);

        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        aTimer.Interval = 5000;
        aTimer.Enabled = true;

        GC.KeepAlive(aTimer);
    }


    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        aTimer.Stop();
        DatabaseUpdation dbUp = new DatabaseUpdation();
        File.AppendAllText(@"C:\Documents and Settings\New Folder\My Documents\demo\abc.txt", "Start" + " " + DateTime.Now.ToString() + Environment.NewLine);
        dbUp.GetDatafromSource();

        aTimer.Start();
    }

}

我从我的 Start 方法中调用它:

    protected override void OnStart(string[] args)
    {
        TimerClass timer = new TimerClass();
    }

但是计时器根本没有执行。谁能在这里找到我的错误?提前致谢

4

4 回答 4

3

请阅读构造函数

您的初始化代码不应公开static void Main(),而应公开public TimerClass()

class TimerClass
{
    private System.Timers.Timer aTimer;

    public TimerClass()
    {
        aTimer = new System.Timers.Timer(1000);
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Interval = 5000;
        aTimer.Enabled = true;
        GC.KeepAlive(aTimer);
    }

    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        aTimer.Stop();
        DatabaseUpdation dbUp = new DatabaseUpdation();
        File.AppendAllText(@"C:\Documents and Settings\New Folder\My Documents\demo\abc.txt", "Start" + " " + DateTime.Now.ToString() + Environment.NewLine);
        dbUp.GetDatafromSource();
        aTimer.Start();
    }
}

您的方法和 aTimer 也不应该是静态的。

于 2013-01-24T09:12:39.943 回答
2

您需要调用Main方法来启动计时器:

protected override void OnStart(string[] args)
{
    TimerClass.Main();
}

顺便说一句,名字不是很好——我认为类似的东西Start会更好。另外我希望这不是您的应用程序入口点方法。

于 2013-01-24T09:15:03.760 回答
1

你忘了启动你的计时器。把它移到你的范围之外。Elapse 仅在您的计时器到期时调用。

aTimer.Start(); 

仅供参考,一旦调用 start 方法,您就不需要启用计时器

于 2013-01-24T09:11:40.857 回答
1

试试这个:

class TimerClass
    {
        private static System.Timers.Timer aTimer;

        public static void Main()
        {

            aTimer = new System.Timers.Timer(1000);

            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            aTimer.Interval = 5000;


            GC.KeepAlive(aTimer);

        }
        public static void Start()
        {
           aTimer.Start();


        }



        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            aTimer.Stop();
            DatabaseUpdation dbUp = new DatabaseUpdation();
            File.AppendAllText(@"C:\Documents and Settings\New Folder\My Documents\demo\abc.txt", "Start" + " " + DateTime.Now.ToString() + Environment.NewLine);
            dbUp.GetDatafromSource();

            aTimer.Start();
        }

    }

你需要这样称呼它:

protected override void OnStart(string[] args)
    {
        //TimerClass timer = new TimerClass();<==No need it's static class!!!
        TimerClass.Main();
        TimerClass.Start();
    }
于 2013-01-24T09:22:22.980 回答