[英]What's the accepted way to handle values that can be different data types in C#?
[英]What's the accepted way to write a C# TSR?
我正在使用C#编写一个WCF路由器,我想在不使用Console.ReadKey“hack”的情况下继续运行。 我想知道是否有一种可以接受的方式在C#中做到这一点,它更优雅
while (true)
Thread.Sleep(100);
创建一个windows service
项目。 与标准Windows客户端相比,Windows服务具有持久的生命周期。
你可以在这里开始吧。
来自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.
服务可以存在三种基本状态之一:正在运行,已暂停或已停止 。 您可以查询状态以确定服务所处的状态。
也许使用ManualResetEvent?
ManualResetEvent stopEvent = new ManualResetEvent(false);
public void StartService()
{
service.Open();
stopEvent.WaitOne()
}
然后,您可以在希望服务终止时执行stopEvent.Set()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.