C# Windows 服务——本地计算机上的服务启动然后停止? [英] C# Windows Service -- the service on local computer started and then stopped?

查看:108
本文介绍了C# Windows 服务——本地计算机上的服务启动然后停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建我的第一个 Windows 服务,但很遗憾...在我从 services.msc 手动启动该服务后,消息本地计算机上的服务已启动然后停止.有些服务会自动停止,因为它们没有工作可做'

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()
    {
    }
}

}

如果我犯了愚蠢的错误,请原谅我.这是我的第一个 Windows 服务.

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'

推荐答案

检查以确保运行服务的帐户可以访问这些文件(包括对 .wav 和 .mp3 文件的写访问权限).

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

您的代码也可能导致未处理的异常.我不确定,但这可能在事件日志中可见.您还可以让您的服务将消息显式地写出到事件日志中(例如在发生异常的情况下);查看此链接:http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

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: http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

这篇关于C# Windows 服务——本地计算机上的服务启动然后停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆