错误 1054 - 服务未及时响应 - C# 应用程序

发布于 2024-12-11 00:04:53 字数 1510 浏览 0 评论 0原文

我在安装服务应用程序时遇到问题。当我运行调试模式时,一切正常,并且所有逻辑内容都有效。我之前写过服务应用程序,比较这两个应用程序,发现这个应用程序和工作应用程序之间没有什么区别。预先感谢您对我的代码的任何帮助:

 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

Im having a problem installing my service application. When I'm running my debug mode it all works correctly and all the logical stuff works. I've written service applications before and comparing the two there is little difference between this one and a working one. Thanks in advance for any help on my code:

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

Ah I found the problem now, the sql connection test was just a quick open and close job but what I didn't see or realise was where I was initializing that _SQLCON object.
I've moved that to my method and works fine now. Happy days, thanks for the answers as it helped me look in the place I wasnt looking. x

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作业与我同在 2024-12-18 00:04:53

最佳实践是调用与服务线程不同的线程上的方法,以避免服务线程阻塞

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

It is a best practise to call to methods on a different thread then the service thread to avoid the blocking of the service thread

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();
}
旧城空念 2024-12-18 00:04:53

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

它在调试中工作,因为您实际上并未将其作为服务启动:

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

我怀疑启动方法由于这一行而花费了很长时间:

    if (_SQLCon.testSQLConnection())

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

The problem is that you are making the database connection in the initialisation of the service itself.

It work in debug because you're not actually starting it as a service:

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

I suspect that the start method is taking a long time because of this line:

    if (_SQLCon.testSQLConnection())

You need to make sure that the start method of the service returns in a timely manner and any potentially long running process is done in a thread. If you move this test into a thread then you should find it working OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文