简体   繁体   English

连续在后台运行控制台应用程序,直到系统关闭

[英]Running a Console App in the Background Continously until System Shutdown

I have a basic app that asks user to 'Press any key or Enter to exit'. 我有一个基本的应用程序,要求用户“按任意键或Enter退出”。 The purpose of implementing my app this way is to have it running till the system shuts down or the user logs out. 以这种方式实现我的应用程序的目的是使其运行直到系统关闭或用户注销。 But I want to have it invisible so the user doesn't accidentally close it when pressing a key intended for another program. 但是我想使其不可见,以便用户在按下用于其他程序的键时不会意外关闭它。 The structure of my program is: 我的程序的结构是:

using System;

namespace myApp
{
  class Program
  {
     private static void Main()
     {
       // Some code here

       // do not close until closed manually
       Console.WriteLine("Press any key to exit");
       Console.ReadKey();
     }

      public static void Method1 ()
      {
         // some code here
      }

      public static void Method2 ()
      {
         // some code for method 2 here
      }
    }
 }

In a similar post, people are suggesting solutions that are either little advanced or do not work for me, eg 在类似的帖子中,人们提出的解决方案要么太先进,要么对我不起作用,例如

  1. Changing into a Windows service application, 更改为Windows服务应用程序,
  2. Running the .exe file of the program as a task, 作为任务运行程序的.exe文件,
  3. Outputting this console app as a Windows app, 将此控制台应用输出为Windows应用,
  4. Starting the VS project as a Windows Form application then not calling any forms, etc. 作为Windows窗体应用程序启动VS项目,然后不调用任何窗体等。

But this does not work for me. 但这对我不起作用。 Let me describe why. 让我描述一下原因。 I have tried options 3 and 4 above, but my app completely disappears/closes, I check the task manager thinking it is may be running in the background but it is not there and that means it closes straight away. 我已经尝试了上面的选项3和4,但是我的应用程序完全消失/关闭,我检查任务管理器以为它可能正在后台运行,但不存在,这意味着它会立即关闭。

I feel changing the app to a windows service will complicate things for my ClickOnce app. 我觉得将应用程序更改为Windows服务会使ClickOnce应用程序变得复杂。 As for 2, people have suggested to open the program as a task and hide the window, but I am not sure how I can do this in the code itself without calling it externally as .exe application. 至于2,人们建议将程序作为一个任务打开并隐藏窗口,但是我不确定如何在代码本身中做到这一点,而无需以.exe应用程序的形式外部调用它。 Thank you in advance. 先感谢您。

You should develop your application as Windows Service . 您应该将应用程序开发为Windows Service

Alternatively you can start your exe by Task Scheduler as background task. 或者,您可以通过Task Scheduler将exe作为后台任务启动。 If you set it as run at startup there will be no ui showed even if it is a form application. 如果将其设置为在启动时运行,即使它是表单应用程序,也不会显示任何用户界面。

This is related to operating system, you can run your console program as follows: 这与操作系统有关,您可以按以下方式运行控制台程序:

Assuming you compile your program as program.exe, you can run this in command line. 假设您将程序编译为program.exe,则可以在命令行中运行它。

START /B program.exe > log.txt

https://superuser.com/questions/198525/how-can-i-execute-a-windows-command-line-in-background https://superuser.com/questions/198525/how-can-i-execute-a-windows-command-line-in-background

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

相关问题 有没有办法从 .net C# 控制台应用程序检测系统关闭并延迟关闭直到您的程序可以正常退出? - Is there a way to detect a System-Shutdown from a .net C# Console App and delay the shutdown until your program can gracefully exit? Shutdown.Netcore IHostedService 作为控制台应用程序 - Shutdown .Netcore IHostedService as Console App 使后台进程在MVC4网站上连续运行 - Have a background process running continously on an MVC4 website C#控制台应用程序系统绘图背景透明度问题 - C# Console App System.Drawing Background Transparency Question 如何在系统 Windows 用户帐户下启动控制台应用程序? - How to start a Console App as running under the System Windows user account? 为什么我的控制台应用程序从System32运行? - Why is my Console app running from System32? 如何保持控制台C#应用程序运行并从标准输入流中读取输入(不连续) - How to keep a console c# application running and reading input from standard input stream (not continously) 如何从控制台/C# 程序启动/关闭 TwinCAT 系统? - How to startup / shutdown TwinCAT System from console / C# program? C#控制台应用程序Environment.Exit(0)无法识别为关闭事件 - C# Console app Environment.Exit(0) not recognised as a shutdown event 使用 async/await 时,我的控制台应用程序过早关闭? - My console app shutdown prematurely when using async / await?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM