1
    #if DEBUG
            MainService service1 = new MainService();
            service1.onDebug();
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
            ConfigurationSync.logDebugMessage(logMessageType.message, "Starting main service thread");

#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new MainService()
            };
            ServiceBase.Run(ServicesToRun);
            ConfigurationSync.logDebugMessage(logMessageType.message, "Starting main service thread");

#endif

I have created my first windows service and I am wondering what the correct way is to start the service in the Program.cs file.

The code supplied above is what was suggested to me by an online tut, #if debug running while testing in Visual Studio and the else block running when built in release mode and installed on the server. The problem is, it doesn't seem to run on the server after install if I don't use the code in the #if debug code block. If i run the code as supplied above it says the service has started but nothing seems to happen, while if I only run what's in the debug block the service runs but I get an error on the server "Service failed to start in a timely manner"

Any help would be appreciated

Update: In my mainservice I have a function that kicks off all the features of the service, this function is startSoftwareUpdates();

These are the functions I have in MainService:

        public MainService()
    {
        startSoftwareUpdates();

        public void onDebug()
    {

        OnStart(null);
    }

    protected override void OnStart(string[] args)
    {
        startSoftwareUpdates();
    }

Update 3:

So I have reshuffled MainService as follows:

        public MainService()
    {
        InitializeComponent();
    }

    public void onDebug()
    {

        OnStart(null);
    }

    protected override void OnStart(string[] args)
    {
        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;
            startSoftwareUpdates();
        }).Start();
    }

What kind of problems might I run into with this approach? I'd like the Thread to run infinitely too...

Tiaan
  • 698
  • 2
  • 10
  • 32
  • What do you *expect* to happen if your service runs? – nvoigt Oct 15 '18 at 16:03
  • Well for one, I'd expect the ConfigurationSync.LogDebugMessage in the else block to fire, which it doesn't. Then I have several other logs located in the mainservice class which also doesn't fire which leads me to believe somewhere within the else block the code breaks... – Tiaan Oct 16 '18 at 13:32
  • 2
    Well you will never see your log because the service never completes the `Run`. That is normal. If your service should be doing things, you should not do it all on start. The method should start up another thread to do the work. – nvoigt Oct 16 '18 at 13:34
  • Well to be clear startSoftwareUpdates(); downloads software updates then fires off several timers, this whole process takes less than a second... – Tiaan Oct 16 '18 at 13:42
  • Processes exit when the last foreground thread exits. If all you're leaving behind after returning from `OnStart` is a *background* thread, that's not going to work well. – Damien_The_Unbeliever Oct 16 '18 at 14:02
  • So Thread.CurrentThread.IsBackground = false; ? – Tiaan Oct 16 '18 at 14:06
  • @Tiaan Have you wrapped your ```startSoftwareUpdates()``` in a ```Try/Catch``` to see if an exception is thrown? – Brandon Oct 16 '18 at 14:48
  • @James yeah I have, there are no exceptions occurring... – Tiaan Oct 16 '18 at 14:57
  • Does ```OnStart``` run without you calling ```OnDebug```? Meaning, if you just instantiate ```MainService```, does is automatically call ```OnStart```? – Brandon Oct 16 '18 at 15:03
  • No it does not, and notably even with nothing other than a log executing in OnStart running sc Start gives an error failed to start in a timely fashion – Tiaan Oct 16 '18 at 15:09
  • Are you still trying to log in the ```Program``` file or did you move that to ```OnStart```? – Brandon Oct 16 '18 at 15:44
  • I have logs in the onStart of the servoce and there's a log before and after I try to start the service in the Program.cs file. – Tiaan Oct 16 '18 at 23:32
  • The log before I try to start the service in Program.cs fires but after that nothing... – Tiaan Oct 16 '18 at 23:33

1 Answers1

3

Nothing looks wrong with your code. If you could post some more of the code that is inside of your MainService() constructor, that'd be helpful considering that could be where the issue is.

However, when I build these services, instead of the #if DEBUG route, I generally do something like this

if (Environment.UserInteractive)
{
      //**** this is for debugging in console mode
}
else
{
     //*** this is for running as a Windows service
     var ServicesToRun = new ServiceBase[]
     {
          new ServiceHome()
     };
     ServiceBase.Run(ServicesToRun);
}

To be clear, I don't expect this to resolve your issue as the code you posted above seems to do the same thing, just in a different manner. I'm posting this to give you a different way to accomplish what you did above.

If I had to guess, I'd say the issue is in your MainService constructor.

Edit

I don't believe the issue is with any of the code that you've written. The error you're getting is caused by a number of potential issues, so it's tough to tell you exactly what it is. Here is a link that has some solutions for you to try. Also, you should try to find the error in the Event Log, assuming you're on a windows machine. That will have a bit more detail to the issue most of the time.

Brandon
  • 3,074
  • 3
  • 27
  • 44
  • 1
    That makes sense, like I said this is my first project so it's possible I was misinformed, I have edited my post to show the code in my MainService – Tiaan Oct 16 '18 at 13:29