[英]What is the best way to keep a console application running in the background?
I am working on a console application that I want to run in the background of machines from the moment they boot to shutdown. 我正在一个控制台应用程序上工作,我希望从启动到关闭的那一刻在计算机的后台运行。 Now I know that it would be optimum to create a Windows Service, but in this case I need to be able to intetact with certificate stores and I do not believe that a local service account certificate store will do.
现在,我知道创建Windows服务将是最佳选择,但是在这种情况下,我需要能够完整地使用证书存储,并且我不相信本地服务帐户证书存储可以做到。
I am looking at System.Timers for keeping the application running rather than bludgening in to death with an infinite while statement, is this a logical way to handle this or is there a better way in which to keep the application open? 我正在使用System.Timers来保持应用程序运行,而不是使用无限的while语句使应用程序陷入瘫痪,这是处理此问题的逻辑方法,还是有一种更好的方法来保持应用程序打开? The application will be checking into a SQL database frequently to see if there is any work to do.
该应用程序将经常检查SQL数据库,以查看是否有任何工作要做。
You can run Windows Services under any user with appropriate privileges. 您可以在具有适当特权的任何用户下运行Windows服务。 You are not required to run it under Local Service account.
您不需要在本地服务帐户下运行它。
Alternatively, you could use a tray icon to keep you application running, see here for a sample . 或者,您可以使用任务栏图标使应用程序运行,请参见此处的示例 。
Although, you can run the application as a windows service it requires additional installation overhead. 尽管您可以将应用程序作为Windows服务运行,但它需要额外的安装开销。 Also = as far as I understand the OP - the application should close when the user logs out, this does not hold true for windows services.
另据我所了解的OP而言-用户注销后应用程序应关闭,但Windows服务并不适用。 So you will have close it explicitly when this behaviour is required.
因此,当需要此行为时,您将显式关闭它。
You could create a windows service and impersonate the user to access the necessary certificate stores. 您可以创建Windows服务并模拟用户以访问必要的证书存储。 See this question: SO Question
看到这个问题: SO问题
Now I know that it would be optimum to create a Windows Service, but in this case I need to be able to intetact with certificate stores and I do not believe that a local service account certificate store will do.
现在,我知道创建Windows服务将是最佳选择,但是在这种情况下,我需要能够完整地使用证书存储,并且我不相信本地服务帐户证书存储可以做到。
In Windows XP: 在Windows XP中:
Start->Settings->Control Panel->Administrative Tools->Services 开始->设置->控制面板->管理工具->服务
Right click on your service->Properties->Log On tab->This Account->Specify account to run as 右键单击您的服务->“属性”->“登录”选项卡->“此帐户”->“指定以以下身份运行”帐户
It's something similar for Windows Vista and Windows 7 but I'm on an XP machine right now. 对于Windows Vista和Windows 7,这有点类似,但是我现在在XP计算机上。
I am looking at System.Timers for keeping the application running rather than bludgening in to death with an infinite while statement, is this a logical way to handle this or is there a better way in which to keep the application open?
我正在使用System.Timers来保持应用程序运行,而不是使用无限的while语句使应用程序陷入瘫痪,这是处理此问题的逻辑方法,还是有一种更好的方法来保持应用程序打开?
A System.Timers
is fine for periodically doing something. System.Timers
适合定期执行某项操作。 Again, you can implement this in a Windows service though. 同样,您可以在Windows服务中实现此功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.