简体   繁体   中英

How do I run my c# program as a scheduled task

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.

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.

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.)

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 .

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.

非常流行的解决方案是 Quartz.NET http://quartznet.sourceforge.net/

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

If I'm understanding your question correctly, this is how you could possibly proceed:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM