C# Windows Service — the service on local computer started and then stopped?


I am trying to create my first Windows Service, but so sad... after I started the service manually from services.msc, the message 'the service on local computer started and then stopped. some services stop automatically is they have no work to do'

I am sure there must be some mistake in my code...

namespace ConvertService { public partial class Service1 : ServiceBase { public Service1() { this.ServiceName = "ConvertService"; this.EventLog.Log = "Application"; } static void main() { ServiceBase.Run(new Service1()); } protected override void OnStart(string[] args) { Process pMP3 = new Process(); pMP3.StartInfo.UseShellExecute = false; pMP3.StartInfo.RedirectStandardOutput = true; pMP3.StartInfo.FileName = @"d:\...path...\converter.exe"; pMP3.StartInfo.Arguments = @"d:\...path...\tempamr.amr " + @"d:\...path...\tempmp3.mp3 " + @"-cmp3"; pMP3.Start(); pMP3.WaitForExit(); Process pWAV = new Process(); pWAV.StartInfo.UseShellExecute = false; pWAV.StartInfo.RedirectStandardOutput = true; pWAV.StartInfo.FileName = @"d:\...path...\converter.exe"; pWAV.StartInfo.Arguments = @"d:\...path...\tempmp3.mp3 " + @"d:\...path...\tempwav.wav " + @"-cwav"; pWAV.Start(); pWAV.WaitForExit(); } protected override void OnStop() { } }


Forgive me if i did silly mistakes. This is my very very first Windows Service.

PS. I have already ticked 'Allow service to interact with desktop'


Check to make sure the account your service runs under can access those files (including write access for the .wav and .mp3 files).

Your code might also be causing an unhandled exception. I'm not sure, but that might be visible in the event log. You can also get your service to write out messages explicitly to the event log (like in the case of an exception); check out this link: <a href="http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx" rel="nofollow">http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx</a>


You didn't create a running thread for the OnStart method. Basically, the service manager calls OnStart to start the service, and that call needs to finish in about 15 seconds or so. Internally, you should create a thread with a loop that actually calls your code over time. Like so:

protected CancellationTokenSource _tokenSource = null; protected Task _thread = null; protected override void OnStart(string[] args) { _tokenSource = new CancellationTokenSource(); _thread = Task.Factory.StartNew(() => DoMyServiceLogic(), TaskCreationOptions.LongRunning, _tokenSource); } protected override void OnStop() { _tokenSource.Cancel(); } protected void DoMyServiceLogic() { while(!_tokenSource.Token.IsCancellationRequested) { // Do Stuff } }

Your service doesn't really follow the pattern; you're not doing things continuously, and that should be more of a console program.

Effectively, it's because your service stopped doing anything as soon as you finished the OnStart method. It's like what happens when you finish Main in a console program - the application just exited.


Open eventvwr.msc. There you will see exception details on why your windows service has stopped working. By the way you should as quickly as possible leave the OnStart method because you only have 30 seconds to finish the OnStart method. There is a excellent article on <a href="http://msdn.microsoft.com/en-us/library/7a50syb3%28v=vs.80%29.aspx" rel="nofollow">MSDN</a> describing "how to debug" Windows Services.