4

My application requires a re-start at midnight time daily. For this job, according to me, the task scheduler would be the ideal thing to use but I haven't been able to find how to use the Task Scheduler APIs in C# (no external libraries to be used)

Once starting the application from Task Scheduler is done, I also wanted suggestions how to shutdown the application automatically at a certain time (even if the app at that particular time might be unresponsive, displaying some error message box or not working due to some problem)

user1240679
  • 6,829
  • 17
  • 60
  • 89

2 Answers2

4

Well, I suppose you don't know about the schtasks command.
You could simply open a command prompt and type schtasks /? to see the options available.
The one needed here is /create. Of course this option requires a series of parameter to configure your scheduled task. But at the end you could resolve all calling:

Process.Start("schtasks", commandParams);

For the close process part, I use a little utility called pskill found in the Sysinternals suite from Microsoft's Mark Russinovich. Also this could be called inside a batch file set as scheduled task.

EDIT: Changed from psshutdown to pskill (You need to close only your app right?)

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks for the reply. I tried to set this task manually through command line first, and after running the `cmd` as administrator, I was able to create this task `schtasks /create /tn "MyApp" /tr c:\app.exe /sc on start` with the message `Successfully created the task`. Apparently, my app requires admin privileges to run and when I restarted the computer, the app didn't run. I checked in schticks /query and the MyApp status was shown as 'Could not start'. Any suggestions for this? – user1240679 Apr 07 '12 at 10:18
  • I don't think it's because of this admin privileges. I tried another simple app which doesn't require admin previliges, but it gave 'Could not start' status when I checked in schtasks /query after restart. – user1240679 Apr 07 '12 at 10:43
  • I changed the task to logon and it runs fine now. :-) – user1240679 Apr 07 '12 at 11:31
  • @Steve Why not just use `Process.Kill`? – Chibueze Opata Apr 07 '12 at 15:08
3

I suggest you write a service to handle this. Also, why does it require a restart?

If however you must use task scheduler, try it like this:

schtasks /create /SC DAILY /TN AppName /TR AppExecutablePath /ST 0
0:00 /ET TIMEFORENDTASK

You can simply type this in a batch script and run the batch script when the user is installing the app, then put this in the uninstall batch script:

schtasks /delete /TN AppName
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
  • Thanks for the reply. I tried to set this task manually through command line first using schtasks` command, and after running the cmd as administrator, I was able to create this task schtasks /create /tn "MyApp" /tr c:\app.exe /sc on start with the message Successfully created the task. Apparently, my app requires admin privileges to run and when I restarted the computer, the app didn't run. I checked in schticks /query and the MyApp status was shown as 'Could not start'. Any suggestions for this? – user1240679 Apr 07 '12 at 10:21
  • Probably you need to pass user name and password of the administrator. See the flags /U /P and also /RU – Steve Apr 07 '12 at 10:28
  • I don't think it's because of this admin privileges. I tried another simple app which doesn't require admin previliges, but it gave 'Could not start' status when I checked in schtasks /query after restart. – user1240679 Apr 07 '12 at 10:43
  • @user1240679: It's `ONSTART` not `ON START`... Since this is on your PC, I suggest you use the GUI, that way you can avoid spelling mistakes or otherwise. Simply type `start taskschd.msc` in command prompt and use the *Create Task Wizard.* – Chibueze Opata Apr 07 '12 at 11:35