How to run a C # program as a scheduled task

I'm still pretty new to C #, so you have to carry with me.

I developed a windows program that 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 the server and simulate a button click in the program to become a scheduled task.

I know how to set up a task on the server side, where you run the program and enter the arguments. But I'm not sure which code I need to include in my program to achieve this.

+7
source share
10 answers

My recommendation would be to get away from running a GUI / windowed application from a planned task - in practice, this is crazy at all. Ideally, deploy a console version of the application that requires execution (possibly with parameter arguments) and does not require interaction with the user (or quasi-user).

If you just can’t create a “system version” of your application, then I think you have two options, both very ugly: 1) create some kind of macro script that runs instead of your program, this script can execute the program and give “click”, 2) perform a “click” at the launch of your application, invoking a button click handler (possibly based on a parameter, to give it duality in execution modes).

+6
source

Consider using the Windows Task Scheduler .

You can extract your business logic into a separate DLL and write a simple console application that will simply launch your task after accepting the parameters via the command line.

+7
source

I think you are also asking about passing command line arguments. See Answers to this question .

In particular, I highly recommend the accepted answer : NDesk.Options .

+2
source

If this is a Windows application, just go to the bin folder, get the executable file, and finally schedule the task for it using the Windows scheduling task and select the exe file as the target application.

if it is a web application, you can include your code in the quartz.net scheduled task, details are on the quartz.net website.

+2
source

A very popular solution is Quartz.NET http://quartznet.sourceforge.net/

+1
source

I have a similar task to make winforms a windows task. i did it

in windows task scheduler in the task tab , under Run put your exe and then / Auto, it will run as a schedule.

Example: winform.exe / Auto

+1
source

If I understand your question correctly, you can continue:

+1
source
0
source

Why not extract the database update logic as a Windows service

you can separate the sql processing part in a separate DLL and use a common DLL for your windows form and service application.

A window is launched in the background and can be automatically started when the computer boots, it can be paused and restarted and no user interface is displayed.

In addition, you do not need to install any third-party software for the same code, and the basic code of the window can be ported to any Windows machine with the installed version of .Net Framework .

0
source

Add Link: 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 
0
source

All Articles