Reputation: 87
I am still pretty much new to c# so you will have to bear with me.
I have developed a windows form program which updates some SQL records as an end of day process for one of our clients.
The next step is that I need to install the program on a server and simulate a button click in the program to become a scheduled task.
I know how to setup the task on the server side where you start program and enter the arguments. But I am unsure as to what code I need to include in my program to achieve this.
Upvotes: 8
Views: 34388
Reputation: 151
Add reference: Microsoft.Win32.TaskScheduler
then write this code:
using (TaskService ts = new TaskService())
Microsoft.Win32.TaskScheduler.Task task = ts.GetTask(TaskName);
task.Run(); //start
task.Stop(); //End
Upvotes: 0
Reputation: 21
If it is a windows application, just go to the bin folder, get the executable file, and finally schedule a task for it by using windows schedule task and choose the exe file as you targeted application.
if it is web application, you may want to include your code in a quartz.net scheduled job, details are on quartz.net website.
Upvotes: 2
Reputation: 1600
Why not extract your database update logic as a windows service
you can segregate the sql handling part in a separate DLL
and use the common DLL
for both your form application and the windows service.
A window service run in background and can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface.
Moreover you need not to install any third party software for same and window service code base can be ported to any windows machine with required version of .Net Framework
installed.
Upvotes: 0
Reputation: 5265
If I'm understanding your question correctly, this is how you could possibly proceed:
Upvotes: 2
Reputation: 914
I have similar task to do making winforms as windows task. what i did is
in windows task scheduler
in the task tab
,under Run
put your exe and then /Auto,it will run as schedule.
Example:winform.exe /Auto
Upvotes: 2
Reputation: 8108
I think you are also asking about command-line argument passing. See the answers to this question.
In particular, I highly recommend the accepted answer: NDesk.Options.
Upvotes: 2
Reputation: 3568
Very popular solution is Quartz.NET http://quartznet.sourceforge.net/
Upvotes: 1
Reputation: 45068
My recommendation would be to get away from running a GUI-based/windowed application from a scheduled task - this is generally madness in practice. Ideally, deploy a console-based version of your application that requires execution (perhaps with parameter arguments) and doesn't require any user (or quasi-user-) interaction.
If you simply can't create a 'system version' of your application, then I guess you have two choices, both immensely ugly: 1) create some kind of macro script which is executed instead of your program, this script could execute the program and issue 'the click', 2) perform 'the click' on startup of your application by invoking the button click handler (maybe based on a parameter to give it a duality in execution modes.)
Upvotes: 8
Reputation: 2523
Take a look in the Timer class
http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx
Upvotes: 0
Reputation: 46008
Consider using Windows Task Scheduler.
You could extract your business logic to a separate DLL and write a simple Console app that will just run your task after accepting the parameters through command line.
Upvotes: 10