Windows服务启动和停止C#

时间:2018-05-22 03:00:27

标签: c# .net windows service

我开发了一项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);
    }

1 个答案:

答案 0 :(得分:1)

查看this walkthrough

我认为您需要在SetServiceStatus方法中调用OnStart方法(来自advapi32.dll),否则服务控制管理器会关闭您的服务,如果需要很长时间才能启动。