C# Windows服务状态始终保持在启动状态,即使它实际已启动

C# Windows服务状态始终保持在启动状态,即使它实际已启动,c#,windows-services,C#,Windows Services,我按照标记答案中的说明创建了一个服务。服务安装正确。当我在一段时间后启动服务时,它会抛出一条消息 “Windows无法在本地计算机上启动xxx服务 错误1053:服务未及时响应启动或控制请求。 " 单击“确定”后,其状态永远保持在“开始”。当我检查应用程序和系统日志时,没有错误 当我检查SQL跟踪时,服务实际上正在正确运行,并且正在做它应该做的事情。那么,为什么它的地位停留在“起点” 更新: 这是OnStart方法中的代码 protected override void OnStart(str

我按照标记答案中的说明创建了一个服务。服务安装正确。当我在一段时间后启动服务时,它会抛出一条消息 “Windows无法在本地计算机上启动xxx服务

错误1053:服务未及时响应启动或控制请求。 "

单击“确定”后,其状态永远保持在“开始”。当我检查应用程序和系统日志时,没有错误

当我检查SQL跟踪时,服务实际上正在正确运行,并且正在做它应该做的事情。那么,为什么它的地位停留在“起点”

更新: 这是OnStart方法中的代码

 protected override void OnStart(string[] args)
        {

            Loader loader = new Loader();
            loader.StartProcess();
        }
更新2:

根据WiktorZychla的评论,我做了这件事,它成功了:)


Main()
函数的Program.cs文件中,确保您具有:

ServiceBase.Run(newserviceclassehere())

我在创建windows窗体应用程序时犯了很多次罪


Application.Run(新类())Main()
函数中的code>在
Main()
函数中的Program.cs文件中确保您具有:

ServiceBase.Run(newserviceclassehere())

我在创建windows窗体应用程序时犯了很多次罪


Application.Run(新类())
在我的
Main()
函数中

准确地了解
OnStart()方法中的内容将是很有帮助的。
OnStart()
方法是来自操作系统的回调,用于启动服务,但它必须在30秒内返回(我记得在某处读过)。否则,操作系统会给出您看到的消息。简而言之,将
OnStart()
方法限制为初始化,并将您的服务要执行的实际工作推迟到某种线程上。

确切了解
OnStart()方法中的内容会很有帮助。
OnStart()
方法是来自操作系统的回调,用于启动服务,但它必须在30秒内返回(我记得在某处读过)。否则,操作系统会给出您看到的消息。简而言之,将
OnStart()
方法限制为初始化,并将您的服务要执行的实际工作推迟到某种线程上。

根据WiktorZychla的评论,我就是这么做的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using IndexLoader;
using System.Threading;

namespace myNameSpace
{
    public partial class LoaderService : ServiceBase
    {
        Thread newThread;
        public LoaderService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            Loader loader = new Loader();

            ThreadStart threadDelegate = new ThreadStart(loader.StartProcess);
            newThread = new Thread(threadDelegate);
            newThread.Start();

        }

        protected override void OnStop()
        {
            if ((newThread != null) && (newThread.IsAlive))
            {


                Thread.Sleep(5000);
                newThread.Abort();

            }
        }
    }
}

根据WiktorZychla的评论,我就是这么做的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using IndexLoader;
using System.Threading;

namespace myNameSpace
{
    public partial class LoaderService : ServiceBase
    {
        Thread newThread;
        public LoaderService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            Loader loader = new Loader();

            ThreadStart threadDelegate = new ThreadStart(loader.StartProcess);
            newThread = new Thread(threadDelegate);
            newThread.Start();

        }

        protected override void OnStop()
        {
            if ((newThread != null) && (newThread.IsAlive))
            {


                Thread.Sleep(5000);
                newThread.Abort();

            }
        }
    }
}

你的出发点是什么?你在纺纱吗?锁定?等待?换句话说:OnStart方法是否完成过?如果
加载程序.StartProcess
从未返回,那么您就拥有了所看到的内容。在OnStart中,您应该启动一个新线程,并且该方法应该在紧接着返回。@WiktorZychla:如果我在OnStart上启动一个新线程,是否需要在OnStart上执行thread.abort?是的,您确实需要这样做(很抱歉,注释太多,但不允许使用短于15个字符的注释)。在OnStart中有什么内容?你在纺纱吗?锁定?等待?换句话说:OnStart方法是否完成过?如果
加载程序.StartProcess
从未返回,那么您就拥有了所看到的内容。在OnStart中,您应该启动一个新线程,并且该方法应该紧接着返回。@WiktorZychla:如果我在OnStart上启动一个新线程,是否需要在OnStart上执行thread.abort?是的,您确实需要这样做(很抱歉,注释太多,但不允许使用小于15个字符的注释)