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.Insta ll;
using System.ServiceProcess;
using Microsoft.Win32;
namespace EventListenerOCS
{
/// <summary>
/// Summary description for ProjectInstaller.
/// </summary>
[RunInstaller(true)]
public class EventListenerOCSInstall : System.Configuration.Insta ll.Install er
{
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.StartTyp e = System.ServiceProcess.Serv iceStartMo de.Automat ic;
/// <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.Open SubKey(
@"SYSTEM\CurrentControlSet \Services\ EventListe nerOCSInst all", 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.InteropServ ices;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Timers;
using EventListenerOCS;
namespace EventListenerOCS
{
public class EventListenerOCS : System.ServiceProcess.Serv iceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont ainer 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.Serv iceBase[] 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.Serv iceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.Serv iceBase[] { new EventListenerOCS() };
System.ServiceProcess.Serv iceBase.Ru n(Services ToRun);
}
/// <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.Cont ainer();
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.ElapsedEvent Handler(Ti merTaskEla psed);
// 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.ElapsedEvent Args 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;
}
}
}
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.Insta
using System.ServiceProcess;
using Microsoft.Win32;
namespace EventListenerOCS
{
/// <summary>
/// Summary description for ProjectInstaller.
/// </summary>
[RunInstaller(true)]
public class EventListenerOCSInstall : System.Configuration.Insta
{
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.StartTyp
/// <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.Open
@"SYSTEM\CurrentControlSet
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"
}
}
}
}
}
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.InteropServ
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Timers;
using EventListenerOCS;
namespace EventListenerOCS
{
public class EventListenerOCS : System.ServiceProcess.Serv
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
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.Serv
// 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.Serv
//
ServicesToRun = new System.ServiceProcess.Serv
System.ServiceProcess.Serv
}
/// <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.Cont
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.ElapsedEvent
// 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.ElapsedEvent
{
// 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;
}
}
}
Have a look at the event log of the machine. It will tell you if the service failed and why.
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()?
m_timer.Start()?
ASKER
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
This eliminated all of my issues
ASKER
I also found this which was helpful as well
http://www.codeproject.com/csharp/SercviceInstallerExt.asp
http://www.codeproject.com/csharp/SercviceInstallerExt.asp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.