C#Windows服务:服务未响应启动 [英] C# Windows Service: The service did not respond to the start

查看:93
本文介绍了C#Windows服务:服务未响应启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试创建调用多个线程的Windows服务.我所做的是创建一个控制台应用程序,然后向其中添加了Windows服务和安装程序.我的代码在下面.

This is my first attempt at creating a Windows service that calls multiple threads. What i did was create a console app, then added a windows service and installer to it. My code is below.

partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        System.Timers.Timer timer1 = new System.Timers.Timer();
        timer1.Interval = 10000;
        timer1.Start();
        timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
    }

    public static void timer1_Elapsed(object sender, EventArgs e)
    {
        Program.ThreadController();   
    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
    }
}

class Program
{
    static void Main(string[] args)
    {
        ThreadController();
    }

    public static void ThreadController()
    {
        for (int i = 0; i < 3; i++)
        {
            new Thread(() => { processEventLogTesting(); });
        }
    }

    public static void processEventLogTesting()
    {
        EventLog.WriteEntry("ProcessThread1", "Process1 Works");
    }
}

我不确定为什么会出现此错误,任何提示都很棒.

I'm not sure why i'm getting this error, any hints would be awesome.

谢谢.

推荐答案

首先,我要添加一些调试功能.其实很容易(当您知道怎么做的时候!!!).

First I'd add in some capability to do debugging. It's actually easy (when you know how!!!).

将此添加到您的班级:

[DllImport("kernel32.dll")]
public static extern Int32 AllocConsole();

然后将其添加到您的主要功能中(如果您使该服务能够顺便访问用户界面,则在作为服务运行时也可以执行此操作.)

Then add this to your main function (and you CAN do this too when running as a service if you enable the service to access the user interface by the way).

for (int loop = 0; loop < args.Count; loop++)
{
  if (args[loop] == "debug")
    AllocConsole();
}

这将为您提供一个控制台窗口,您现在可以使用Console.WriteLine将输出发送到该窗口.好吃.

This gives you a console window and you can use Console.WriteLine to send output to it now. Yummy.

现在,您要实现的是让服务控制管理器调用OnStart方法,然后该方法必须返回OK信号,以使SCM知道您的应用程序已正常启动.否则,SCM认为出了点问题,并杀死了您的应用程序.因此,OnStart应该启动您的线程.因此,创建一个由线程(和Main)运行的Run()方法,并使OnStart尽快返回.

Now what you want to achieve, is for the service control manager to call your OnStart method, and then that method must RETURN the OK signal to let the SCM know that your app started up ok. Otherwise, SCM thinks something is wrong and kills your app. So OnStart is supposed to start your threads. So, make a Run() method that gets run by your thread (and by Main), and make OnStart return as fast as you can.

我注意到在您的代码示例中我没有看到任何这些内容:

I notice I don't see any of this in your code example:

System.ServiceProcess.ServiceBase [] ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase [] {new WinService1()}; System.ServiceProcess.ServiceBase.Run(ServicesToRun);

System.ServiceProcess.ServiceBase[] ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinService1() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun);

我怀疑您的应用没有向SCM报告它已成功启动了可执行文件中的服务".

I suspect that your app isn't reporting to the SCM that it has successfully launched the "service" that is inside your executable.

您希望您的应用启动,将服务"交给SCM,让它知道您还可以,然后返回,而您的服务现在正在使用其自己的线程运行.

You want your app to fire up, hand off the "service" to the SCM and let it know you are ok, and then return, while your service is now running with its own thread.

请参阅本文,以获取出色的演练: http://msdn.microsoft.com/zh- us/library/aa984464(v = vs.71).aspx

See this article for a great walk through: http://msdn.microsoft.com/en-us/library/aa984464(v=vs.71).aspx

这篇关于C#Windows服务:服务未响应启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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