How to make a program run in the background (invisible) in Windows?

palmboy5

Limp Gawd
Joined
Oct 19, 2006
Messages
315
I have a really simple command line program I wrote that I want to run without the need for a Command Prompt/PowerShell window to be left open. Basically, the only place I should find it is in the Processes tab in Task Manager. How can I do this?
 
Thanks for your reply. So how do I run it as a service (automatically at startup for that matter)?
 
No need for a service; just spawn the program with a hidden window.

You need to think carefully about why you're doing this. Users don't like mysterious things running without identifying themselves, showing their progress, or clearly expressing errors.
 
What are you trying to accomplish with the PS script? It isn't something that can just run as a scheduled task?
 
Sorry, I should have tried Google searching the service thing first. As an excuse though, I thought the query was one of those peculiar queries that have no exact, helpful results. The spawning thing is one of those peculiar queries.

My program is not a script, but only because I haven't really written a script before so I didn't try. This is my program.
Code:
#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
   FILE *fp = 0;
   
   while (1) {
      fp = fopen("abc.txt", "w");
      fprintf(fp, "Some content.\n");
      fclose(fp);
      Sleep(5000);
      remove("abc.txt");
      Sleep(5000);
   }
   return 0;
}
Now your question is definitely going to be "what is the point of this??".
The new hard drive that I got for my laptop (WD3200BEVT) because I wanted a quieter hard drive, likes to make a clicking/thunking sound a couple times per minute when idle, and I find it really annoying. It is not the same as normal activity clicking, it is much louder. Apparently it is normal behavior since there are plenty of reports about the noise in the Newegg user reviews. I should have read the reviews more thoroughly before buying. SO, to try to avoid RMAing the HDD, I am first going to try running that program 24/7 to prevent the HDD from ever idling and thus prevent it from making the thunking noise.

Naturally this is a kind of program that I just want in the background where I can forget about it until the next reformatting of the laptop. I will try making it a service ASAP.
 
Last edited:
lol... i'm pretty sure there is a way to prevent the drive from spinning down already in Windows.
 
I'd write a TSR that continuously executes command line chkdsk, and wrap that in a Windows service that executes cmd.
 
The new hard drive that I got for my laptop (WD3200BEVT) because I wanted a quieter hard drive, likes to make a clicking/thunking sound a couple times per minute when idle, and I find it really annoying. It is not the same as normal activity clicking, it is much louder. Apparently it is normal behavior since there are plenty of reports about the noise in the Newegg user reviews. I should have read the reviews more thoroughly before buying. SO, to try to avoid RMAing the HDD, I am first going to try running that program 24/7 to prevent the HDD from ever idling and thus prevent it from making the thunking noise.

Isn't that just going to chew up your battery life?
 
lol... i'm pretty sure there is a way to prevent the drive from spinning down already in Windows.
The drive is NOT spinning down, I never even let the HDD ever spin down. The drive is simply making a very annoying noise when idle.

Isn't that just going to chew up your battery life?
I don't think so... why? My program has virtually no CPU usage and the HDD activity LED doesn't even blink for such a small operation.
*Tests*
EDIT: Nope.
 
Last edited:
I don't think so... why? My program has virtually no CPU usage and the HDD activity LED doesn't even blink for such a small operation.
*Tests*
EDIT: Nope.
I think the concern was more from the HDD chewing up your battery life by never going into a lower power mode (ie: not idle). If battery life is a concern for you, then you really should do a long term test over several hours -- both with and without the app running.
 
The concern, actually, is treating the symptom rather than the problem.
 
Well, I never allow the HDD to spin down in the first place so whatever sort of low power mode it can still enter probably doesn't have that much of a significance in terms of power drain. Most importantly though, during times when the laptop is running on battery power, I will actually be giving it full usage since at that time it will be the only computer at my disposal. This means that, for example, my AIM's live logging of my chats will serve the purpose of my program in the first place. My program's purpose is to fill in any time when my HDD may have otherwise gone idle. Without wanting to develop it into something that can detect when no other programs are using the HDD, I'm just giving it a relatively ample 5 second gap between writes. (BTW, I run my computers 24/7)

The concern, actually, is treating the symptom rather than the problem.
Could you elaborate please?

EDIT:
I finally went and made the program a service and it seems to be working, thanks guys!
 
Last edited:
Could you elaborate please?
Your drive is making noises. Treating the problem would involve doing an RMA on the drive, or replacing it. Instead, you're writing a program that makes it make less noise, which addresses the symptom and causes more problems.
 
The solution is not so clear-cut. Please know that I value silence greatly. I spent $65 getting the HDD for my laptop to be quieter than it was before (SSDs are too expensive). $65, on eliminating some noise. I chose the WD Scorpio Blue HDD because it is apparently as quiet as 2.5" HDDs can get, and it is indeed noticeably quieter, both in the whooshing spin sound and in the activity clicking, than my former Toshiba drive. The problem is just the infernal clicking noise the WD HDD does when idle that IMO undoes the HDD's supposed quiet superiority.
Too many user reviews on newegg mention this clicking noise, so even if the noise is still a defect, the likelihood of me getting a non-defective by replacing is too small.
My choices are:
1.) Run my program, which is now successfully invisible (thanks again guys).
2.) RMA the drive and find an alternative drive.
3.) Swap the Toshiba back from the PS3 into the laptop and put the WD into the PS3.

Each choice's problem(s):
1.) I don't think my program is fully stopping the click. I have heard the click a couple times (vast improvement) these past few days while my program was running. Program tweaks are needed, maybe with varying content sizes.
2.) RMA shipping cost as well as cost of shipping a louder alternative drive. Not to mention the resulting laptop down time.
3.) I am back where I started with my laptop and it is not guaranteed that the PS3 will insulate the HDD enough for me to not hear the same clicking coming from the PS3.

Right now I AM seriously considering option 2, but if I can successfully get my program to stop the clicking, I don't see what the problem is with keeping the drive like so.
 
You don't need to make it a service.

Use the WinAPI. It doesn't require you to make a Window, but will allow to have a taskbar icon. The exact function calls elude me now though.
 
If anyone cares, I've finally recorded some "footage" of the HDD clicks I've been complaining about.
http://www.mylilsite.net/audio/wd3200bevtReducedNoiseAmplified.mp3
I got rid of most of the background noise and amplified it, but the volume at which the click is heard in my mp3 is irrelevant because it may or may not represent the real life volume depending on your current volume settings. That is something I cannot control and will not try to. The purpose is just to show what the noise sounds like.
 
Why don't you make a recording so we can hear what it sounds like to open an RMA?
 
Why don't you find a WD3200BEVT that does not make that noise? EVERY one does that noise, RMA doesn't do anything.

My program already stopped 90% of the clicks and it took minutes to code while costing nothing. This is better than a fruitless quest in replacing it with either another clicking identical drive or another model that wouldn't have been as quiet overall anyway. I already said this.
 
I do this for some scripts I like to run in the background. This script will execute a powershell script in the background with no Window or taskbar icon, and the only way to see it or kill it is in task manager:

Code:
if ($args.count -ne 1) {write-host "Requires 1 Argument, exiting...";sleep 2; break}
$a = (get-item -ea silentlycontinue $args[0]).fullname
if ($a -eq $null) {write-host "$($args[0]) is not a valid location, exiting...";sleep 2;break}
 
 
# Specifies a set of values that are used when you start a process.
$si = new-object System.Diagnostics.ProcessStartInfo
$si.fileName = "powershell.exe"
$si.Arguments= "-nologo -command `"$($a)`""
$si.WorkingDirectory = $pwd
# this will launch the process in the background (hidden)
$si.windowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
# run process under alternate credentials
# $si.UserName="username"
# $si.Password="Password"
 
# start the process
$process = New-Object System.Diagnostics.Process
$process.startInfo=$si
 
# this is also valid
# $process = [System.Diagnostics.Process]::Start($si)
 
# close notepad and watch the output
#if($process.start()) { $process.waitForExit(); "existed"}  
 
$process.start()

To use it, make a shortcut that targets:
Code:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "c:\users\jgentile\powershell\hideprocess.ps1" "c:\users\jgentile\powershell\<PUTSCRIPTNAMEHERE>.ps1"

hideprocess.ps1 is the name of the script I posted above, save it to a file and put the path\name at the appropiate place in the shortcut, put the other script where I have <putscriptnamehere> and change the directory to an appropiate directory for your profile or whatever.

When you run it you will see a powershell window open then disappear, then your program will be running in the background.
 
Why don't you find a WD3200BEVT that does not make that noise?
Sounds like you bought the wrong drive, then, if you're concerned about noises. Maybe you should choose a different model.
 
For all the time that's been spent looking into this, he could have easily earned enough money to buy the quietest driive in the world.
 
I didn't spend much time... most of the time on this topic was actually spent as forum activity, repeating (again and again) my reasons to people like mikeblas. (More importantly, dude, I never asked for opinions on what to do, I asked how to make something run in the background.) Making my program a service works well enough for me, and was finished over a month ago.
 
I'm not the only person who thinks it's a marginal, backward idea. If you're happy with the solution, that's great--but it makes the matter even more amusing, really.
 
Back
Top