代码之家  ›  专栏  ›  技术社区  ›  dlanod

什么是公认的写C#TSR的方法?

  •  3
  • dlanod  · 技术社区  · 15 年前

    while (true)
        Thread.Sleep(100);
    
    2 回复  |  直到 15 年前
        1
  •  8
  •   this. __curious_geek    15 年前

    创建 windows service 项目。与标准的windows客户机相比,windows服务具有持久的生命周期。

    here .

    来自msdn:

    加载服务后,必须启动它。启动服务允许它开始工作。您可以从服务控制管理器、服务器资源管理器或通过调用start方法从代码启动服务。Start方法将处理传递给应用程序的OnStart方法,并处理在其中定义的任何代码。

    A running service can exist in this state indefinitely until it is either stopped or paused or until the computer shuts down. 服务可以存在于 三种基本状态:运行、暂停或停止 . 您可以查询状态以确定服务处于什么状态。

        2
  •  3
  •   mfanto    15 年前

    ManualResetEvent stopEvent = new ManualResetEvent(false);
    
    public void StartService()
    {
    
         service.Open();
         stopEvent.WaitOne()
    }
    

    然后,您可以在希望服务终止时执行stopEvent.Set()。