保持C#控制台应用程序运行的最佳方法是什么 [英] What is the best way to keep a C# Console app running

查看:76
本文介绍了保持C#控制台应用程序运行的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个控制台应用程序,该应用程序必须在后台运行.该应用程序必须不断检查数据库中是否有新记录.如果返回了新记录,则将对其进行处理.我目前的应用程序使用while(true)来保持应用程序运行.使用while循环是最好的解决方案.

I have developed a console app which must run in the background. The app must constantly check a database for new records. If new records are returned they are processed. My app as it currently stands uses while(true) to keep the application running. Is using a while loop the best solution.

我的代码段:

static void Main(string[] args)
{
  while(true)
  {
    // Query db for new records
    if(record_count > 0)
    {
      // Process the records
    }
    Thread.Sleep(500);
  }
}

推荐答案

创建为Windows服务应用程序

Create as Windows Service Application

什么是Windows服务?

  • 使您能够创建在其自己的Windows会话中运行的长期运行的可执行应用程序.
  • 可以在计算机启动时自动启动,可以暂停和重新启动,而无需任何用户交互.
  • 通过运行命令行实用程序InstallUtil.exe并将路径传递到服务的可执行文件,可以轻松安装.

为什么要使用Windows服务?

在某些企业中,最常见的要求之一是根据一定的时间间隔长时间运行计划作业.例如:每天下午发送时事通讯,或者每隔一小时向客户发送一封电子邮件警报.

One of the most common requirements of some businesses is long-running scheduled jobs based on some time interval. For example: sending a newsletter everyday afternoon or send an email alert to the customer for every one hour.

因此,构建Windows服务可能是实现目标的可靠解决方案之一,该目标可以在后台完成所需的工作而不会干扰同一台计算机上的其他用户.

So building a Windows Service could be one of the reliable solutions to do the goal that can do the required job in the behind the scenes without interfering others users on the same computer.

请参考链接以逐步实施-

Please refer the link for step by step implementation -

http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/

这篇关于保持C#控制台应用程序运行的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆