Hi Guys,

I have written a windows c# service that basically write out some text to a file in the onStart function. The program works fine when i manually start or stop it. Now if i leave it started and restart my system, the service does not seem to do anything (i.e nothing written to the log file). I have tried both the release and the debug builds. And i have installed the service correctly. Could you tell me if I am missing any required step so that the service runs properly after the system startup? Thanks in advance.

- Anup

The InitializeComponent function

private void InitializeComponent()
{
	this.eventLog1 = new System.Diagnostics.EventLog();
	((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
	// 
	// eventLog1
	// 
	this.eventLog1.Log = "DoDyLog";
	this.eventLog1.Source = "DoDyLogSource";
	// 
	// Service1
	// 
	this.ServiceName = "MyNewService";
	((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

}

The OnStart Function

protected override void OnStart(string[] args)
{
	eventLog1.WriteEntry("my service started");
}

the OnStop function

protected override void OnStop()
{
	eventLog1.WriteEntry("my service stoped");
}

The service is set to start automatically. When i start it, it writes to event log. I leave it started and then log off and log back in, i see the the service shows started but there is nothing written to the event log.

I am using windows vista home premium 64 bit edition.

Did you read the URL I posted?

Here is a quote:
I think the problem may be that you try to write to the EventLog but the
EventLog service it self is not started yet.

You need to mark your service as "depends on event log service" so it will not execute until the event log service has started. Please read the URL I posted as it answers your question.

And the URL one more time:
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic15625.aspx

hey guys...well i just created a setup project to install my service and bang it was working after a restart. I don't know why it is but now it works fine. Thanks for all you inputs.

If your service writes to the event log on startup and you didn't add the "event log" service as a dependency then it is probably a roll of the dice whether or not your service will run with each reboot. If the eventlog service starts up fully before your service does then everything is OK. If it doesn't then it will lock up. I don't know the service start order but this doesn't sound like a reliable solution to me. I'm glad it worked for this reboot but I doubt you have solved the problem.

What I expect is: the service writes something in file and because it doesn't perform any task again it stopped and service should do something while running..

OnStart(...)
{
InfiniteLoop();
}
InfiniteLoop()
{
for(;;)
{
WriteData(filePath, DateTime.Now.ToShortTimeString());
Thread.Stop(10000);
}
}

What I expect is: the service writes something in file and because it doesn't perform any task again it stopped and service should do something while running..

Ramy,
I don't think that is the problem though. This is code from the OP:

protected override void OnStart(string[] args)
{
	eventLog1.WriteEntry("my service started");
}

So we all agree he is writing to the event log when the service starts AND that the service starts on boot, right?

Now from an article I posted earlier they offered three causes for this:

First --

think the problem may be that you try to write to the EventLog but the
EventLog service it self is not started yet.

You have to make sure, your service is not started before the EventLog
service started successfully. Thus you have to make your service dependent
on the EventLog service.

Second --

Based on my understanding, you implemented an autostart windows service
application in .Net, which works well, but it sometimes can not auto start
on certain machines.

Based on my experience, the problem is probably due to that the Application
event log is full and it does not allow overwriting old entries. Under such
circumstances, the call to the EventLog.WriteEntry in our code cannot
succeed and the service fails to start eventually.

Third --

We had the very same problem - .NET service failed to start
automatically during machine reboot (timeout) but started without
problems when started later manually.
This seems to be a potential problem for any .NET service on a slower
machine (or heavy loaded one) - it just takes some time for CLR to
translate from MSIL to native code and after restart many other
services are being started simultaneously, so sometimes the default
timeout of 30 seconds is not enough :(

The solution was quite simple: we increased the timeout in registry
(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServicesPipeTimeout)
and since then our service starts without problems

@Scott, Can they send the code to review it.
@Serkan, Let's discuss it in Daniweb feedback I think we may deserve it.

This article has been dead for over six months. Start a new discussion instead.