简体   繁体   English

保持C#应用程序运行

[英]Keep C# application running

I'm building a Windows Service that uses FileSystemWatcher, and runs in the background. 我正在构建一个使用FileSystemWatcher的Windows服务,并在后台运行。

I don't want to keep on uninstalling and installing the service every time I want to debug, so would like to do most of my development in a normal program before moving it into a service. 我不想每次调试时都继续卸载和安装服务,因此在将其移入服务之前,我希望在常规程序中进行大部分开发。 But I'm quite new to this, and when I run it, it just runs through the block and exits. 但是我对此很陌生,当我运行它时,它只是贯穿整个块并退出。

What would be a good way to keep the program running? 保持程序运行的好方法是什么?

http://einaregilsson.com/run-windows-service-as-a-console-program/ http://einaregilsson.com/run-windows-service-as-a-console-program/

I've used this before to debug my service as a Console application based on whether its running in an interactive user environment. 之前,我已根据服务是否在交互用户环境中运行将其调试为控制台应用程序。

public partial class DemoService : ServiceBase
{
    static void Main(string[] args)
    {
        DemoService service = new DemoService();

        if (Environment.UserInteractive)
        {
            service.OnStart(args);
            Console.WriteLine("Press any key to stop program");
            Console.Read();
            service.OnStop();
        }
        else
        {
            ServiceBase.Run(service);
        }
    }
while (true)
{
  // Execute your program's functionality here.
}

I wrote a 7 part series a while ago titled: Building a Windows Service . 不久前,我写了一个由7部分组成的系列文章: 构建Windows服务 It covers all the intricacies of building services, making them friendly to debug, and self-installing. 它涵盖了构建服务的所有复杂性,使它们易于调试和自动安装。

The basic feature set I was looking for was as follows: 我正在寻找的基本功能集如下:

  • Building a service that can also be used from the console 构建也可以从控制台使用的服务
  • Proper event logging of service startup/shutdown and other activities 正确记录服务启动/关闭和其他活动的事件
  • Allowing multiple instances by using command-line arguments 使用命令行参数允许多个实例
  • Self installation of service and event log 自行安装服务和事件日志
  • Proper event logging of service exceptions and errors 正确记录服务异常和错误的事件
  • Controlling of start-up, shutdown and restart options 控制启动,关闭和重新启动选项
  • Handling custom service commands, power, and session events 处理自定义服务命令,电源和会话事件
  • Customizing service security and access control 定制服务安全性和访问控制

The final result was a Visual Studio project template that creates a working service, complete with all of the above, in a single step. 最终结果是一个Visual Studio项目模板,可在一个步骤中创建可完成上述所有操作的服务。 It's been a great time saver for me. 对我来说,这节省了很多时间。

see Building a Windows Service – Part 7: Finishing touches for a link to the project template and install instructions. 有关指向项目模板和安装说明的链接,请参见构建Windows服务–第7部分:润色

Here's documentation from MSDN @ http://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.80).aspx?ppud=4 . 这是来自MSDN的文档@ http://msdn.microsoft.com/zh-cn/library/7a50syb3(v=vs.80).aspx?ppud=4 I have tried it before and it works under .NET Framework 3.x. 我之前尝试过,它在.NET Framework 3.x下可以工作。 I could not find my descriptive notes on it, at the moment. 目前,我找不到关于它的描述性注释。

Use the pragma #If DEBUG for debugging purposes like console outputs. 使用pifma #If DEBUG进行调试,例如控制台输出。 Another is using the Debug object. 另一个是使用Debug对象。

If you have any trouble with this, say so. 如果您对此有任何疑问,请这样说。 I may be able to find my notes or make a Windows Service app myself, just to see if the steps on MSDN still work. 我也许可以找到我的笔记或自己制作Windows Service应用程序,只是为了查看MSDN上的步骤是否仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM