3

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

Jumbo
  • 533
  • 3
  • 8
  • 15
  • 1
    in order to answer this question its important to know what framework you are using: MFC, .NET, or just the native Win32 API? – Chris Becke Dec 27 '10 at 07:02

4 Answers4

4

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

Otherwise, just create an application without a window.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
3

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.

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 ShellExecute and ShellExecuteEx functions. The argument nShowCmd (or nShow) gives you (among others) the options:

SW_HIDE

Hides the window and activates another window.

SW_SHOWMINNOACTIVE

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

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

Community
  • 1
  • 1
  • This is very advanced way to achieve this and super non intuitive, but effective... You can also use this simple program http://www.joeware.net/freetools/tools/quiet/index.htm if you are in a hurry and don't have time to program it yourself. I tested it, and it works for batch scripts just fine, too. – ScienceDiscoverer Jun 06 '22 at 20:15
1

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

Brian Clapper
  • 25,705
  • 7
  • 65
  • 65
  • 2
    this answer would apply if this was superuser.com – Chris Becke Dec 27 '10 at 07:08
  • 1
    This is not "programmatically". – Cody Gray - on strike Dec 27 '10 at 11:19
  • He can certainly "programmatically" create a Windows service, or otherwise arrange to put the process in the background. On the other hand, he could also "programmatically" simply arrange to use the START command. You can defined "programmatically" how you see fit, but sometimes, the simplest solution (use the existing command infrastructure) fits, regardless of how you choose to parse "programmatically." – Brian Clapper Dec 27 '10 at 14:20
  • @BrianClapper No, this will not work. Start command /b option cannot start fully hidden batch script or executable file, it only opens it inside the current console window. Moreover, you can't use START command with a system() function from the program itself. Program cannot run itself through START command, only some other program. Only way to do this is either to make it a Service or via ShellExecute (also works for batch scripts just fine). – ScienceDiscoverer Jun 06 '22 at 20:13
-1

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