我开发了一项Windows服务,通过使用线程并在ms SQL数据库中插入,从指纹机中不时获取指纹考勤数据。当我在本地计算机上运行该服务时,它工作正常但是当我在服务器2012中安装并运行它时,服务开始然后停止。请帮助我。
protected override void OnStart(string[] args)
{
new LogManagement().WriteToLog("Service Started -" + DateTime.Now);
new LogManagement().WriteToLog("Thread Started" + DateTime.Now);
RunThread();
}
protected override void OnStop()
{
new LogManagement().WriteToLog("Service Stopped -" + DateTime.Now);
}
public void RunThread()
{
Thread thread = new Thread(new ThreadStart(DeviceChecker));
thread.IsBackground = true;
thread.Name = "DeviceCheckingThread";
thread.Start();
}
private void DeviceChecker()
{
//Process reading data and insert into database
Thread.Sleep(10000);
}
答案 0 :(得分:1)
我认为您需要在SetServiceStatus
方法中调用OnStart
方法(来自advapi32.dll),否则服务控制管理器会关闭您的服务,如果需要很长时间才能启动。