Windows service with timer

前端 未结 3 1535
迷失自我
迷失自我 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:

    
        
            
                1
            
        
    
    

提交回复
热议问题