Windows service with timer

前端 未结 3 1449
迷失自我
迷失自我 2020-11-27 05:18

I have created a windows service with timer in c#.net. it works fine while i debug/build the project in visual studio but it does not perform its operation after installatio

相关标签:
3条回答
  • 2020-11-27 05:26

    Here's a working example in which the execution of the service is started in the OnTimedEvent of the Timer which is implemented as delegate in the ServiceBase class and the Timer logic is encapsulated in a method called SetupProcessingTimer():

    public partial class MyServiceProject: ServiceBase
    {
    
    private Timer _timer;
    
    public MyServiceProject()
    {
        InitializeComponent();
    }
    
    private void SetupProcessingTimer()
    {
        _timer = new Timer();
        _timer.AutoReset = true;
        double interval = Settings.Default.Interval;
        _timer.Interval = interval * 60000;
        _timer.Enabled = true;
        _timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    }
    
    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        // begin your service work
        MakeSomething();
    }
    
    protected override void OnStart(string[] args)
    {
        SetupProcessingTimer();
    }
    
    ...
    }
    

    The Interval is defined in app.config in minutes:

    <userSettings>
        <MyProject.Properties.Settings>
            <setting name="Interval" serializeAs="String">
                <value>1</value>
            </setting>
        </MyProject.Properties.Settings>
    </userSettings>
    
    0 讨论(0)
  • 2020-11-27 05:31

    You need to put your main code on the OnStart method.

    This other SO answer of mine might help.

    You will need to put some code to enable debugging within visual-studio while maintaining your application valid as a windows-service. This other SO thread cover the issue of debugging a windows-service.

    EDIT:

    Please see also the documentation available here for the OnStart method at the MSDN where one can read this:

    Do not use the constructor to perform processing that should be in OnStart. Use OnStart to handle all initialization of your service. The constructor is called when the application's executable runs, not when the service runs. The executable runs before OnStart. When you continue, for example, the constructor is not called again because the SCM already holds the object in memory. If OnStop releases resources allocated in the constructor rather than in OnStart, the needed resources would not be created again the second time the service is called.

    0 讨论(0)
  • 2020-11-27 05:40

    First approach with Windows Service is not easy..

    A long time ago, I wrote a C# service.

    This is the logic of the Service class (tested, works fine):

    namespace MyServiceApp
    {
        public class MyService : ServiceBase
        {
            private System.Timers.Timer timer;
    
            protected override void OnStart(string[] args)
            {
                this.timer = new System.Timers.Timer(30000D);  // 30000 milliseconds = 30 seconds
                this.timer.AutoReset = true;
                this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
                this.timer.Start();
            }
    
            protected override void OnStop()
            {
                this.timer.Stop();
                this.timer = null;
            }
    
            private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                MyServiceApp.ServiceWork.Main(); // my separate static method for do work
            }
    
            public MyService()
            {
                this.ServiceName = "MyService";
            }
    
            // service entry point
            static void Main()
            {
                System.ServiceProcess.ServiceBase.Run(new MyService());
            }
        }
    }
    

    I recommend you write your real service work in a separate static method (why not, in a console application...just add reference to it), to simplify debugging and clean service code.

    Make sure the interval is enough, and write in log ONLY in OnStart and OnStop overrides.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题