1

I Have following code, which does not executed when windows service starts

I found solutions here but they didn't work for me.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration;
using System.Threading.Tasks;

namespace TFS_JIRA_sync
{
    public partial class SyncProcess : ServiceBase
    {

        public SyncProcess()
        {
            InitializeComponent();
        }

        private System.Timers.Timer timer = new System.Timers.Timer();

        protected override void OnStart(string[] args)
        {

            this.timer.Interval = ScanPeriod.period * 60000; //turn minutes to miliseconds            
            this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);//OnTimer;            
            this.timer.Enabled = true;
            this.timer.AutoReset = true;
            this.timer.Start();
        }

        private void OnTimer(object sender, System.Timers.ElapsedEventArgs e)
        {
            Processing proc = new Processing();
            proc.doProcess();
        }

        protected override void OnStop()
        {
        }
    }
}

In Programm:

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

[assembly: log4net.Config.XmlConfigurator(Watch = true)]


namespace TFS_JIRA_sync
{
    static class Program
    {
        /// <summary>
        /// Главная точка входа для приложения.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new SyncProcess() 
            };
            ServiceBase.Run(ServicesToRun);

            //Processing proc = new Processing();
            //proc.doProcess();

        }
    }
}

When I comment part starting "ServiceBase[]..." and uncomment "Processing ..." in Programm class it works fine.

But when my code run as Windows service - Nothing happens

Ilya
  • 101
  • 2
  • 13
  • Looks correct (other than setting [`Enabled`](https://msdn.microsoft.com/en-us/library/system.timers.timer.enabled(v=vs.110).aspx) **and** calling [`Start`](https://msdn.microsoft.com/en-us/library/system.timers.timer.start(v=vs.110).aspx)). How have you determined that it's *not* operating correctly? – Damien_The_Unbeliever Oct 27 '15 at 07:10
  • in Processing class I have Logger, which logs steps of executing . When I start my Programm as Win service nothing happens in log – Ilya Oct 27 '15 at 07:17

2 Answers2

0

Put Console.ReadLine() in your code:

  static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new SyncProcess() 
        };
        ServiceBase.Run(ServicesToRun);    
        Console.ReadLine();
    }
  • That certainly won't help if they're trying to get their code to run as a service. The uncommented bits of `Main` that they're showing is the *standard* `Main` for a service application, created from the "Windows Service" template in VS. – Damien_The_Unbeliever Oct 27 '15 at 07:23
0

As I see your service is not going to run all the time this.timer.Interval = ScanPeriod.period * 60000;. For your scenario rather than having a windows service with a timer (and spending time on resolving the issue), I'd recommended to use a scheduled task (as suggested by this answer). It references a Jon Gallow's article, that gives a lot of reasons why it is better to use a scheduled task.

If you're writing a Windows Service that runs a timer, you should re-evaluate your solution.

Community
  • 1
  • 1
Andrei Mihalciuc
  • 1,950
  • 12
  • 12