Schedule a C# console application

24,820

Solution 1

If you want to manage the scheduler by code have a look at Quartz.NET: http://quartznet.sourceforge.net/

This library provides the creation of jobs and triggers.

e.g. (directly from: http://quartznet.sourceforge.net/tutorial/lesson_3.html):

// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();

// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();

// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(DumbJob));
// fire every hour
Trigger trigger = TriggerUtils.MakeHourlyTrigger();
// start on the next even hour
trigger.StartTime = TriggerUtils.GetEvenHourDate(DateTime.UtcNow);  
trigger.Name = "myTrigger";
sched.ScheduleJob(jobDetail, trigger);

Now this means your console application should run continuously which makes it not really suitable for the job.

Option 1. Add a task via task scheduler (real easy) that executes your console application. See example task: http://www.sevenforums.com/tutorials/12444-task-scheduler-create-new-task.html

Option 2. Create a windows service (not to complicated) that uses a library like Quartz.NET or .NET's Timer class to scheduele jobs and executes a batch operation. See for creation of windows service http://msdn.microsoft.com/en-us/library/zt39148a.aspx

Option 3. Make your console application implement a scheduele library like Quartz.NET or /Net's Timer class and run it as a service (a bit more complex): Create Windows service from executable

Solution 2

As long as it requires no user input, it will be able to run on it's own. You can schedule the program to run in Windows Task Scheduler for example.

Share:
24,820
SuicideSheep
Author by

SuicideSheep

"If you see scary things, look for the helpers- you'll always see people helping" -Fred Rogers

Updated on July 09, 2022

Comments

  • SuicideSheep
    SuicideSheep almost 2 years

    I've given a task to create an email C# console application which targeted to run as batch. I'm very new to C# area and hence I have no idea about my direction. This C# console application will be deployed on a server and expect to run in certain time based on server time.

    After some research, most of the suggestions are about using task scheduler or window services, but I'm wondering if this C# console application is somehow possible to run own its own? Maybe after execute it, it then register itself into the server and the server will handle it periodically?

  • SuicideSheep
    SuicideSheep over 10 years
    This console application is somewhat tricky, it should be having 2 options which is daily and yearly and the code to be executed accordingly. Meaning it requires the Windows Task Scheduler to actually pass in the parameter? Is that even possible?
  • Arran
    Arran over 10 years
    @IsaacLem, of course, have you tried it yourself? There's an Add arguments box to use when you create a task. Alternatively, why not use a configuration file of some sort?
  • Gerrie Schenck
    Gerrie Schenck over 10 years
    Yes task scheduler can pass on fixed parameters as command line attributes. If I understand correctly you will have one task which should be ran once a year? Then create a task for this calling yourapp.exe /year. Then another for daily which is called with another parameter.
  • SuicideSheep
    SuicideSheep over 10 years
    @Arran: Yes I saw that Add arguments and I'm wondering how my C# console application get to read the argument?
  • Arran
    Arran over 10 years
    @IsaacLem, you know the Main method in your program? It defaults to accepting a parameter named args. This string array will contain the arguments you are passing in. It's just up to you to read them and do something depending on what you've been given.