【问题标题】:Error 1054 - Service did not respond in Timely Fashion - C# Application错误 1054 - 服务没有及时响应 - C# 应用程序
【发布时间】:2011-12-11 16:00:46
【问题描述】:

我在安装我的服务应用程序时遇到问题。当我运行我的调试模式时,它一切正常,所有逻辑的东西都正常工作。我以前写过服务应用程序,比较两者,这个和一个工作的应用程序之间几乎没有区别。提前感谢您对我的代码的任何帮助:

 class MainClass : ServiceBase
{
    ABCSQLCon _SQLCon = new ABCSQLCon();
    private int cycleTime = 0;

    private delegate void processError(String errorMessage);

    static void Main(string[] args)
    {
        #if(!DEBUG)
            ServiceBase.Run(new MainClass());
        #else
            MainClass service = new MainClass();
            service.OnStart(new string[0]);
        #endif
    }

    protected override void OnStart(string[] args)
    {
        addToLog("Testing SQL Connection...", "Log");
        cycleTime = _SQLCon.sleepTime;
        addToLog("Sleep Time has been set...", "Log");
        if (_SQLCon.testSQLConnection())
        {
            addToLog("Connection to SQL Database succeeded", "Log");
           // queryThread();

            //not neccessary to make applicated multithreaded yet.
            addToLog("Starting Query Thread...", "Log");
            ThreadStart queryCycle = new ThreadStart(queryThread);
            Thread qThread = new Thread(queryCycle);
            qThread.Start();
        }
    }

    private void startProgram()
    {
    }

    protected override void OnStop()
    {
        base.OnStop();
    }

    public MainClass()
    {
        this.ServiceName = "ABCSQL Engine";
    }

啊,我现在发现了问题,sql 连接测试只是一个快速打开和关闭的工作,但我没有看到或意识到我在哪里初始化那个 _SQLCON 对象。 我已将其移至我的方法中,并且现在可以正常工作。快乐的日子,感谢您的回答,因为它帮助我找到了我没有找到的地方。 x

【问题讨论】:

  • 请澄清问题所在 - 您的问题不清楚。
  • testSQLConnection() 可能需要很长时间才能完成。服务控制管理器只给你很短的时间来完成OnStart 方法,然后它就会推断它已挂起。

标签: c# service saas


【解决方案1】:

最好的做法是调用与服务线程不同的线程上的方法以避免服务线程的阻塞

public void MyMethod()
{
        addToLog("Testing SQL Connection...", "Log");
        cycleTime = _SQLCon.sleepTime;
        addToLog("Sleep Time has been set...", "Log");
        if (_SQLCon.testSQLConnection())
        {
            addToLog("Connection to SQL Database succeeded", "Log");
           // queryThread();

            //not neccessary to make applicated multithreaded yet.
            addToLog("Starting Query Thread...", "Log");
            ThreadStart queryCycle = new ThreadStart(queryThread);
            Thread qThread = new Thread(queryCycle);
            qThread.Start();
        }

}

 private Thread _myThread;
 protected override void OnStart(string[] args)
{
        ThreadStart threadStart = MyMethod;
         _myThread = new Thread(threadStart);
         _myThread.Start();
}

【讨论】:

  • 啊我现在发现了问题,sql 连接测试只是一个快速打开和关闭的工作,但我没有看到或意识到我在哪里初始化那个 _SQLCON 对象。我已经将 _SQLCON 的初始化移到了我的方法中,现在可以正常工作了。快乐的日子,感谢您的回答,因为它帮助我找到了我没有找到的地方。 x
【解决方案2】:

问题是您在服务本身的初始化过程中建立了数据库连接。

它可以在调试中工作,因为您实际上并没有将它作为服务启动:

    #if(!DEBUG)
        ServiceBase.Run(new MainClass());
    #else
        MainClass service = new MainClass();
        service.OnStart(new string[0]);
    #endif

我怀疑 start 方法因为这条线而花费了很长时间:

    if (_SQLCon.testSQLConnection())

您需要确保服务的启动方法及时返回,并且任何可能长时间运行的进程都在线程中完成。如果您将此测试移动到一个线程中,那么您应该会发现它工作正常。

【讨论】:

    猜你喜欢
    • 2023-01-13
    • 2021-03-13
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2014-08-05
    相关资源
    最近更新 更多