#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...