如何在后台运行 .NET 控制台应用程序

发布于 2024-08-30 06:31:36 字数 127 浏览 4 评论 0原文

我有一个用 C# 编写的控制台应用程序,计划使用内置的 Windows 任务计划程序每 15 分钟左右运行一次。

每次运行时,黑色控制台框都会在执行期间弹出,然后关闭。我没有向控制台写入任何内容。有没有办法让这个在后台运行?

I have a console application written in C# that is scheduled to run every 15 minutes or so using the built-in Windows Task Scheduler.

Every time it runs, the black console box pops up for the duration of its execution and then closes. I am not writing anything to the console. Is there a way to make this run in the background?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

舞袖。长 2024-09-06 06:31:36

项目>>属性>应用程序选项卡>将输出类型更改为“Windows 应用程序”。

不再有控制台窗口。


.NETCore 又名 .NET5+ 的更新:编辑项目文件并将 OutputType 更改为 WinExe

Project > Properties> Application tab > change Output type to "Windows application".

No more console window.


Update for .NETCore aka .NET5+: edit the project file and change OutputType to WinExe.

十六岁半 2024-09-06 06:31:36

简单的!

这似乎令人难以置信,但它确实具有魅力。当您想要在没有任何迹象的情况下执行自定义任务时,我已将其用于一些设置项目。

  • 将项目创建为 Windows 应用程序项目(这是困难的部分)。
  • 切勿调用任何表单。只需像在控制台应用程序中一样继续操作即可

    类程序
    {
        静态无效主(字符串[]参数)
        {
            // 只是不要调用 Application.Run(new frmMain(args));
    
            // ...你的代码
        }
     }
    

这是因为 Windows 应用程序项目与控制台没有真正的不同,除了第一个表单和引用之外。
这是完全隐藏的执行。试试吧!

Easy!

It seems hard to believe, but it works as a charm. I have used this for some setup projects, when you want to perform custom tasks with no signs of it.

  • Create the project as a Windows application project (this is the hard part).
  • Never make calls to any form. Just keep on in exactly as in your console application

    class Program
    {
        static void Main(string[] args)
        {
            // Just don't call Application.Run(new frmMain(args));
    
            // ... your code
        }
     }
    

This is because windows application projects are no really different than console, except because of the first form and references.
It is totally hidden execution. Try it!

如何视而不见 2024-09-06 06:31:36

您可以使用 Windows API 来最小化控制台框。否则,您可以将其设为实际上不加载表单的 Windows EXE 文件并调用 System.Windows.Forms.Application.Run()。

最小化控制台的代码:

[DllImport( "user32.dll" )]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

public const int SW_SHOWMINIMIZED = 2;

IntPtr winHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(winHandle, SW_SHOWMINIMIZED);

You can use the Windows API to minimize the console box. Otherwise you can make it a Windows EXE file that does not actually load a form and call System.Windows.Forms.Application.Run().

Code to minimize the console:

[DllImport( "user32.dll" )]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

public const int SW_SHOWMINIMIZED = 2;

IntPtr winHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(winHandle, SW_SHOWMINIMIZED);
找回味觉 2024-09-06 06:31:36

将应用程序实现到 Windows 服务中怎么样?您可以将间隔设置为 15 分钟并在timer_tick 中运行该操作。

What about implementing the app into a windows service? You can set the interval to 15 mins and run the operation in the timer_tick.

·深蓝 2024-09-06 06:31:36

如果您已经创建了 Windows 控制台应用程序,则只需将应用程序的输出类型更改为 Windows 应用程序即可。

在您的项目下:
转到属性>应用
选择“Windows 应用程序”作为输出类型。

这将是最小的影响,您可以让 Windows 任务计划程序运行相同的任务。

If you already have a Windows Console app created you can simply change the Output type of your app to Windows Application.

Under your project:
Go to Properties > Application
Select "Windows Application" as the Output type.

This would be the least impact and you can keep your Windows Task Scheduler running the same task.

尤怨 2024-09-06 06:31:36

如果它没有向控制台写入任何内容,您可以将其设为服务。
http://msdn.microsoft.com/en-我们/库/9k985bc9%28VS.80%29.aspx

If it doesn't write anything to the console you could make it a service.
http://msdn.microsoft.com/en-us/library/9k985bc9%28VS.80%29.aspx

智商已欠费 2024-09-06 06:31:36

仅当计划以当前登录的同一用户身份运行时,它才会显示。使用长得可笑的密码在计算机上创建另一个用户,将其设置为管理员(仅在需要时)并安排任务以该用户身份运行用户。

It will only show up if it's scheduled to run as the same user that's currently logged in. Create another user on the machine with a ridiculously long password, set it to be an Administrator (only if needed) and schedule the task to run as that user.

反目相谮 2024-09-06 06:31:36

这很容易。将任务设置为在非您的登录帐户的帐户下运行。

This is easy. Set the task to run under an account that is not your login account.

穿透光 2024-09-06 06:31:36

您可以使用 https://github.com/winsw/winsw

WinSW 将任何应用程序作为Windows 服务:

  1. 编写 myapp.xml(有关更多详细信息,请参阅 XML 配置文件规范和示例)。
  2. 运行 winsw install myapp.xml [options] 以安装该服务。
  3. 运行 winsw start myapp.xml 启动该服务。
  4. 运行 winsw status myapp.xml 以查看您的服务是否已启动并正在运行。

You can use https://github.com/winsw/winsw

WinSW wraps and manages any application as a Windows service:

  1. Write myapp.xml (see the XML config file specification and samples for more details).
  2. Run winsw install myapp.xml [options] to install the service.
  3. Run winsw start myapp.xml to start the service.
  4. Run winsw status myapp.xml to see if your service is up and running.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文