Saturday, April 07, 2012 7:49 AM
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)
Perhaps, I would have to use the COM reference for the Task Scheduler, but it would be good if somebody could point to a simple example on how to use this in C#, so I could get started.
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)
Saturday, April 07, 2012 8:18 AM
for Automatic shutdown you can use Timer Class.
please have a look at http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)] public class Timer : Component, ISupportInitialize
New Ideas, Superior Solutions Www.SalamGroup.Net
Saturday, April 07, 2012 9:11 AM
You may refer to the source code of this open source project to learn about how to wrap the COM interfaces,
That project is released under MIT/X11 license, so you can use its source code freely (even cut and paste).
However, for simple requirements like yours, calling command line utilities to set up scheduled tasks may be an easier approach,
To achieve the re-start, you may write a separate command line utility which iterates all running processes, finds yours, kills them, and then restarts them (if in C#, Process class provides all necessary functions). Then scheduling this utility to run at midnight can fulfill your requirements.
- Marked As Answer by optimus_prime Wednesday, April 11, 2012 4:52 PM
Saturday, April 07, 2012 10:20 AM
Thanks for the reply. Even I was looking at schtasks
for this purpose and 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 onlogon with the message `Successfully created the task`.
EDITFrom my application, to create the task I use
However, the task gets created only if I run my application as an administrator. I need to avoid this, so that any user can create the task without the hassle of running the app as admin. Any suggestions on how ca this be done?
string args = @"/Create /tn MyApp /tr c:\myapp.exe /sc onlogon";
Saturday, April 07, 2012 11:57 PM
Even if you wrap Task Scheduler COM interfaces, administrator permission is required.
To workaround that, you will have to use impersonation (run schtasks using an administrator account), or create a Windows service (running under Local System) to call schtasks.
Lex Li (http://lextm.com)
Monday, April 09, 2012 4:03 AM
Monday, April 09, 2012 6:33 AM
you can also refer this link for task scheduler in c#................
for shut down and restart your system...
for task scheduler
Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights