错误 1054 - 服务未及时响应 - C# 应用程序
我在安装服务应用程序时遇到问题。当我运行调试模式时,一切正常,并且所有逻辑内容都有效。我之前写过服务应用程序,比较这两个应用程序,发现这个应用程序和工作应用程序之间没有什么区别。预先感谢您对我的代码的任何帮助:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最佳实践是调用与服务线程不同的线程上的方法,以避免服务线程阻塞
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
问题是您在服务本身的初始化中建立数据库连接。
它在调试中工作,因为您实际上并未将其作为服务启动:
我怀疑启动方法由于这一行而花费了很长时间:
您需要确保服务的启动方法及时返回并且任何可能长时间运行的进程都是在线程中完成的。如果您将此测试移至线程中,那么您应该会发现它工作正常。
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:
I suspect that the start method is taking a long time because of this line:
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.