简体   繁体   English

编写C#TSR的可接受方式是什么?

[英]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.

相关问题 在C#中处理可以是不同数据类型的值的公认方法是什么? - What's the accepted way to handle values that can be different data types in C#? 在C#中,进行构造函数链接的最佳/可接受方式是什么? - In C#, what is the best/accepted way of doing constructor chaining? 在C#中将short []数组写入文件的最佳方法是什么? - What's the best way to write a short[] array to a file in C#? 在C#中将HTML写入WebBrowser的正确方法是什么? - What's the proper way to write HTML to a WebBrowser in C#? 在C#中编写[0..100]的最佳方法是什么? - What's the best way to write [0..100] in C#? 在C#中编写批处理脚本的好方法是什么? - What's a good way to write batch scripts in C#? 目前可以使用C#4.0从SQL Server检索数据的方法是什么? - What is the currently accepted way to retrieve data from a SQL Server using C# 4.0? 在C#中,在类中引用成员属性的普遍接受的方法是什么? - In C#, what is the generally accepted way to refer to member properties within the class? 使用受保护对象C#编写单元测试的好方法是什么(使用NMock和NUnit框架) - What's a good way to write Unit tests for code with protected objects C# (using NMock, and NUnit framework) 分发具有.NET依赖关系的Excel模板的公认方法是什么? - What's the accepted way to distribute an Excel template with .NET dependencies?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM