7

I have written a small console program that checks that all my websites are up and running properly. I use the Microsoft Windows Task Scheduler to run this program automatically once a day.

Screenshot

The problem is that, quite naturally, the program steals focus every time it runs. I would prefer if it would start without becoming the top-most window on my desktop. What is the simplest way to achieve this? Of course, I could rewrite my EXE to create a background window (or no window at all), but it would be nice if the Task Scheduler had an option like "Do not steal focus" or something, but maybe that is not even possible...

Chindraba
  • 1,983

3 Answers3

6

You could have Task Scheduler run a vbscript that opens your console program. The vbscript can use the Shell Run method to open it minimized with something like:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe", 6, true

There is some basic info about this at http://ss64.com/vb/run.html

Geo
  • 106
  • 1
    I'd avoid VBScript purely because it is insecure and some shops don't allow it at all. However, this solution will work, so I'm voting it up. –  Apr 25 '12 at 17:27
  • This works very well! I'll accept it. – Andreas Rejbrand Apr 25 '12 at 17:55
  • 1
    On Windows 7 I'd recommend PowerShell (Start-Process). – Bob Apr 25 '12 at 18:48
  • I've used a method similar to this in the past to make windows invisible. Good call. – Melikoth Apr 25 '12 at 19:25
  • 1
    This unfortunately prevents the Task Scheduler from correctly stopping the task and detecting if instances are already running. Long-running repeatable tasks frequently start running in parallel. May be a problem. – Axel Fontaine Jul 28 '12 at 09:52
2

Don't call the application directly. Run it from a shortcut that starts minimized.

I suppose you could also add a batch file into the mix that uses /start /min for the same effect, but I suspect the batch file will steal focus unless it is called from a shortcut that starts minimized.

EDIT: The start /min does steal focus very briefly, but it works on my machine, using a .cmd batch file that calls a new Command Line window (using cmd.exe).

My batch file looks like this:

@echo off
cls
start /min C:\Users\Randolph\Desktop\shortcut.lnk

The shortcut itself opens cmd.exe and has default options selected, except that it opens minimized (the option Run: is set to Minimized).

  • Strangely, this doesn't work. I have created a shortcut that starts the EXE minimized, and when I doubleclick the shortcut (LNK file), the EXE is indeed started minimized. But when I tell Task Scheduler to start the LNK, the EXE is still not minimized. – Andreas Rejbrand Apr 25 '12 at 17:34
  • That's interesting. What happens with the start /min option? And does the task have the same privileges that you do? I'm wondering if this is to do with interacting with the desktop like some services do, where the permissions are slightly different. Geo's suggestion with VBScript may be the way to go. –  Apr 25 '12 at 17:37
  • I've just noticed this: when I create a task using a shortcut, it ignores the shortcut and goes directly to the target. Perhaps this is what's happening with you and you need to paste the path to the lnk directly into the Program/Script box? –  Apr 25 '12 at 17:42
  • I noticed that too, but I wasn't fooled by it. I had to write ahs.lnk manually in the task's properties. But still... Also, the BAT approach doesn't work. For some unknown reason, when run, the task opens a console window with the exe file as title, but the contents of cmd. – Andreas Rejbrand Apr 25 '12 at 17:43
  • The start /min worked on my computer, I'm sorry to say. I don't know what to suggest now. –  Apr 25 '12 at 17:47
  • Hm... A BAT that opens a LNK that opens an EXE? I just tried the VBS approach and it works perfectly, so I am afraid that I'll accept that answer. But thank you very much for your time! – Andreas Rejbrand Apr 25 '12 at 17:54
  • No problem at all. I had a .cmd (not .bat) that opened the EXE. It works, but it's certainly not pretty! –  Apr 25 '12 at 17:55
0

This syntax allows the start /min idea to work:
%comspec% /c start "" /min "C:\Scripts\Destination_inbound_ftp5.bat"

P Pace
  • 21