仅在方法完成后才重新运行C#Windows服务

时间:2017-09-15 19:19:12

标签: c# service

我有一个Windows服务问题。我正在编写一个带有计时器的服务,它将耗费一些时间并不断重复。

在我的代码中,我希望timer1_Tick方法仅在完成后重新运行,而不是每2秒后重新运行一次。我怎样才能实现它?有什么指针吗?

这是我的代码的外观:

using System;
using System.ServiceProcess;
using System.Timers;

namespace TestWindowsService
{
    public partial class Scheduler : ServiceBase
    {
        private Timer timer1 = null;
        public Scheduler()
        {
            InitializeComponent();
        }

        public void onDebug()
        {
            OnStart(null);
        }

        protected override void OnStart(string[] args)
        {

            timer1 = new Timer();
            this.timer1.Interval = 2000; //every 2 seconds
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
            timer1.Enabled = true;
            Helper.ServiceStartLog("Test window service started");
        }

        private void timer1_Tick (object sender, ElapsedEventArgs e)
        {
            PDFHelper.WriteErrorLog("Timer ticker and Log Running Job is Running");
        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
            Helper.WriteErrorLog("Test window service stopped");
        }
    }
}

Program.cs看起来像:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestWindowsService
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {

#if DEBUG
            Scheduler myservice = new Scheduler();
            myservice.onDebug();
            Thread.Sleep(Timeout.Infinite);

#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Scheduler()
            };
            ServiceBase.Run(ServicesToRun);
#endif
        }
    }
}

帮助程序类似乎

using System;
using System.IO;
using System.Threading;

namespace TestWindowsService
{
    public static class Helper
    {

        public static void ServiceStartLog(string Message )
    {
        try
        {
            StreamWriter sw = null;
            sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\logFile.txt", true);
            sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
            sw.Flush();
            sw.Close();
        }

        catch (Exception ex1)
        {
            Console.WriteLine(ex1.Message.ToString());

        }
    }

        public static void WriteErrorLog(string Message)
        {
            try
            {
                StreamWriter sw = null;
                Thread.Sleep(5000);
                sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\logFile.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
                sw.Flush();
                sw.Close();
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

将计时器的AutoReset属性设置为false,这将使计时器仅运行一次,然后在计时功能结束时重新启动计时器。

protected override void OnStart(string[] args)
{

    timer1 = new Timer();
    this.timer1.Interval = 2000; //every 2 seconds
    this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
    timer1.AutoReset = false;
    timer1.Enabled = true;
    Helper.ServiceStartLog("Test window service started");
}

private void timer1_Tick (object sender, ElapsedEventArgs e)
{
    try
    {
        PDFHelper.WriteErrorLog("Timer ticker and Log Running Job is Running");
    }
    finally
    {
        //this gets run even if there was a exception.
        timer1.Start();
    }
}