C # Windows service - service on local computer started and then stopped?

I try to create my first Windows service, but it’s so sad ... after I started the service manually with services.msc, the message "the service on the local computer started and then stopped. Some services automatically stop if they don’t have work"

I am sure there should be some kind of error 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 made stupid mistakes. This is my very first Windows service.

PS. I already noted "Allow the service to interact with the desktop"

0
source share
3 answers
0

OnStart. OnStart, , 15 . , . :

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
     }
}

; - , .

, -, OnStart. , , Main - .

+3

eventvwr.msc. , Windows . , OnStart, 30 , OnStart. MSDN, "" Windows.

0
source

All Articles