[英]What's the accepted way to write a C# TSR?
I'm using C# to write a WCF router which I want to stay running without using the Console.ReadKey "hack". 我正在使用C#编写一个WCF路由器,我想在不使用Console.ReadKey“hack”的情况下继续运行。 I was wondering if there's an accepted way to do this within C# that's more elegant than
我想知道是否有一种可以接受的方式在C#中做到这一点,它更优雅
while (true)
Thread.Sleep(100);
create a windows service
project. 创建一个
windows service
项目。 windows services have persistent lifecycle as compared to standard windows client. 与标准Windows客户端相比,Windows服务具有持久的生命周期。
You can get started here . 你可以在这里开始吧。
From msdn: 来自msdn:
After the service has been loaded, it must be started. 加载服务后,必须启动它。 Starting the service allows it to begin functioning.
启动服务允许它开始运作。 You can start a service from the Services Control Manager, from Server Explorer, or from code by calling the Start method.
您可以从服务控制管理器,服务器资源管理器或通过调用Start方法从代码启动服务。 The Start method passes processing to the application's OnStart method and processes any code you have defined there.
Start方法将处理传递给应用程序的OnStart方法,并处理您在那里定义的任何代码。
A running service can exist in this state indefinitely until it is either stopped or paused or until the computer shuts down.
A service can exist in one of three basic states: Running, Paused, or Stopped . 服务可以存在三种基本状态之一:正在运行,已暂停或已停止 。 You can query the Status to determine what state a service is in.
您可以查询状态以确定服务所处的状态。
Maybe use a ManualResetEvent? 也许使用ManualResetEvent?
ManualResetEvent stopEvent = new ManualResetEvent(false);
public void StartService()
{
service.Open();
stopEvent.WaitOne()
}
You can then do a stopEvent.Set() when you want the service to terminate. 然后,您可以在希望服务终止时执行stopEvent.Set()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.