Link to home
Start Free TrialLog in
Avatar of NewMom2Brandon
NewMom2Brandon

asked on

ASAP HELP NEEDED Windows Service set to automatic (start up) but doesn't start everytime after rebooting PC

I have a windows service that is supposed to start automatically. Well when I reboot the PC it does not start everytime.

I am not sure why this is happening. Does anyone have any suggestions on what I can do to make this work.

here is the installer file

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using Microsoft.Win32;

namespace EventListenerOCS
{
   /// <summary>
   /// Summary description for ProjectInstaller.
   /// </summary>
   [RunInstaller(true)]

   public class EventListenerOCSInstall : System.Configuration.Install.Installer
   {
      ServiceInstaller serviceAdmin = new ServiceInstaller();

      public const int MSGBOXONTOPKEY  =  256;

      /// <summary>
      /// Required designer variable.
      /// </summary>
      public QTIEventListenerOCSInstall()
      {
         // TODO: Add any initialization after the InitializeComponent call

         process.Account = ServiceAccount.LocalSystem;
         
         serviceAdmin.StartType      = ServiceStartMode.Automatic;
         serviceAdmin.ServiceName    = "QTIEventListenerOCS";
         serviceAdmin.DisplayName    = "QTIEventListenerOCS";

//         this.serviceAdmin.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
         
         /// <Summary >
         // Now just add the installers that we created to our
         // parents container.
         /// </Summary >
         Installers.Add( process );
         Installers.Add( serviceAdmin );

         // Here is where we set the bit on the value in the registry.
         // The 256 value will enable the messagebox to stay on top of all running
         // applications. However it may not be supported from Microsoft with newer
         // operating systems.
         RegistryKey ckey = Registry.LocalMachine.OpenSubKey(
            @"SYSTEM\CurrentControlSet\Services\EventListenerOCSInstall", true);
         if(ckey != null)
         {
            // Ok now lets make sure the "Type" value is there,
            //and then do our bitwise operation on it.
            if(ckey.GetValue("Type") != null)
            {
               ckey.SetValue("Type", ((int)ckey.GetValue("Type") | MSGBOXONTOPKEY));
            }
         }
      }
   }
}



here is the other file

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Timers;

using EventListenerOCS;

namespace EventListenerOCS
{
   public class EventListenerOCS : System.ServiceProcess.ServiceBase
   {
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;
      private ServerSocket server;
      protected Thread              m_thread;
      protected ManualResetEvent    m_shutdownEvent;
      protected TimeSpan            m_delay;
      private System.Timers.Timer m_timer = null;

      public static bool m_bIsDisplayed = false;    

      /// <summary>
      /// This is used for the messagebox. If displayed or not (True/False)
      /// </summary>
      public static bool m_IsDisplayed
      {
         get { return m_bIsDisplayed; }
         set { m_bIsDisplayed = value;}
      }

      public EventListenerOCS()
      {
         // This call is required by the Windows.Forms Component Designer.
         InitializeComponent();

         // TODO: Add any initialization after the InitComponent call
      }

      // The main entry point for the process
      static void Main()
      {
         System.ServiceProcess.ServiceBase[] ServicesToRun;
      
         // More than one user Service may run within the same process. To add
         // another service to this process, change the following line to
         // create a second service object. For example,
         //
         //   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
         //
         ServicesToRun = new System.ServiceProcess.ServiceBase[] { new EventListenerOCS() };

         System.ServiceProcess.ServiceBase.Run(ServicesToRun);
      }

      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         components = new System.ComponentModel.Container();
         this.ServiceName = "QTIEventListenerOCS";
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
         if( disposing )
         {
            if (components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose( disposing );
      }

      /// <summary>
      /// Set things in motion so your service can do its work.
      /// </summary>
      protected override void OnStart(string[] args)
      {
         // TODO: Add code here to start your service.
         // Create the timer if one is needed.
         m_timer = new System.Timers.Timer();
         m_timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerTaskElapsed);

         // Always wait a minute on startup so the service starts correctly on reboot.
         m_timer.Interval = 120000;  
         //m_timer.Start();
         m_timer.Enabled = true;
         
         server = new ServerSocket();
         server.StartServer();
      }

      protected void TimerTaskElapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
         // The timer will be started again once tasks are completed.
         //m_timer.Stop();
         m_timer.Enabled = false;
      }
 
      /// <summary>
      /// Stop this service.
      /// </summary>
      protected override void OnStop()
      {
         // TODO: Add code here to perform any tear-down necessary to stop your service.
         // The timer will be started again once tasks are complete.
         //m_timer.Stop();
         m_timer.Enabled = false;
         // Stop the Server. Release it.
         server=null;
      }
   }
}
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Have a look at the event log of the machine. It will tell you if the service failed and why.
Avatar of NewMom2Brandon
NewMom2Brandon

ASKER

I just get a timed out message
You may want to check the dependencies of the service... If your service depends on another ( like SQL Server ) it may not be able to start until the other start and times out while waiting.
Where are you starting the timer?
m_timer.Start()?
I found out the problem. The place I am trying to start the service on has the desktop locked out. So the only way I can make the service start on reboot. is to change it from automatic to manual and run a batch file.

This eliminated all of my issues
I also found this which was helpful as well

http://www.codeproject.com/csharp/SercviceInstallerExt.asp
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial