如何在后台运行 .NET 控制台应用程序
我有一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
项目>>属性>应用程序选项卡>将输出类型更改为“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
.简单的!
这似乎令人难以置信,但它确实具有魅力。当您想要在没有任何迹象的情况下执行自定义任务时,我已将其用于一些设置项目。
切勿调用任何表单。只需像在控制台应用程序中一样继续操作即可
这是因为 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.
Never make calls to any form. Just keep on in exactly as in your console application
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!
您可以使用 Windows API 来最小化控制台框。否则,您可以将其设为实际上不加载表单的 Windows EXE 文件并调用 System.Windows.Forms.Application.Run()。
最小化控制台的代码:
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:
将应用程序实现到 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.
如果您已经创建了 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.
如果它没有向控制台写入任何内容,您可以将其设为服务。
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
仅当计划以当前登录的同一用户身份运行时,它才会显示。使用长得可笑的密码在计算机上创建另一个用户,将其设置为管理员(仅在需要时)并安排任务以该用户身份运行用户。
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.
这很容易。将任务设置为在非您的登录帐户的帐户下运行。
This is easy. Set the task to run under an account that is not your login account.
您可以使用 https://github.com/winsw/winsw
WinSW 将任何应用程序作为Windows 服务:
myapp.xml
(有关更多详细信息,请参阅 XML 配置文件规范和示例)。winsw install myapp.xml [options]
以安装该服务。winsw start myapp.xml
启动该服务。winsw status myapp.xml
以查看您的服务是否已启动并正在运行。You can use https://github.com/winsw/winsw
WinSW wraps and manages any application as a Windows service:
myapp.xml
(see the XML config file specification and samples for more details).winsw install myapp.xml [options]
to install the service.winsw start myapp.xml
to start the service.winsw status myapp.xml
to see if your service is up and running.