在 windows 中创建后台进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4536416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:51:39  来源:igfitidea点击:

Create a background process in windows

windowsvisual-c++processbackground-process

提问by Jumbo

How do I make a process go the background programatically?

如何以编程方式使进程进入后台?

What I want is for the user to double-click the process executable, and it just goes into the background ... and does not open a window while executing.

我想要的是让用户双击进程可执行文件,它只是进入后台......并且在执行时不打开窗口。

Any code snippet in visual c++ would be very helpful

Visual C++ 中的任何代码片段都会非常有帮助

回答by Cody Gray

Have you considered creating a Windows Serviceinstead? They're specifically designed to run in the background without showing a UI.

您是否考虑过创建Windows 服务?它们专门设计为在后台运行而不显示 UI。

Otherwise, just create an application without a window.

否则,只需创建一个没有 window 的应用程序

回答by Brian Clapper

This might help: http://ss64.com/nt/start.html

这可能会有所帮助:http: //ss64.com/nt/start.html

回答by Adam Douglass

I know this is old, but I thought I would post something for when people find this through search.

我知道这是旧的,但我想我会发布一些东西,当人们通过搜索找到它时。

While I like Cody Gray's answer for design correctness, sometimes you don't have a choice.

虽然我喜欢 Cody Gray 对设计正确性的回答,但有时您别无选择。

If you want to launch a program without jumping to the new window (it appears in the background or minimized) or not create a window at all try looking at the ShellExecuteand ShellExecuteExfunctions. The argument nShowCmd(or nShow) gives you (among others) the options:

如果您想启动程序而不跳转到新窗口(它出现在后台或最小化)或根本不创建窗口,请尝试查看ShellExecuteShellExecuteEx函数。参数nShowCmd(or nShow) 为您提供(除其他外)选项:

SW_HIDE

Hides the window and activates another window.

SW_SHOWMINNOACTIVE

Displays the window as a minimized window. The active window remains active.

SW_HIDE

隐藏窗口并激活另一个窗口。

SW_SHOWMINNOACTIVE

将窗口显示为最小化窗口。活动窗口保持活动状态。

As the documentation says, SW_HIDEcreates a process running the executable you give it, but if this program would normally create a window, none appears.

正如文档所说,SW_HIDE创建一个运行您提供的可执行文件的进程,但如果该程序通常会创建一个窗口,则不会出现任何窗口。

回答by user3451413

I tried this way and it worked fine: Create a console application and write your codes in the sub main as any other console application. Now change the application type in the project properties to windows Forms application from Console application thats it

我试过这种方法,效果很好:创建一个控制台应用程序,并像任何其他控制台应用程序一样在 sub main 中编写代码。现在将项目属性中的应用程序类型从控制台应用程序更改为 windows Forms 应用程序