4

I have a C# console application. I would like to run that every hour of the day. This application is not intended to run on my local machine but on other users machine as a background process. I don't want the user to take the trouble of setting a task manually using task scheduler. How can I achieve the same for him ?

user2901683
  • 175
  • 1
  • 1
  • 12

3 Answers3

4

You can set it up to run on a timer with an interval of 1 hour.

See this link for examples How do you add a timer to a C# console application

You could also install it as a windows service on the user's machine.

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135
  • 1
    A service is the way to go. – Dan Oct 31 '13 at 04:35
  • what about http://taskscheduler.codeplex.com/ It seems like everyone of these shall work. Could you suggest which one should be easy to implement ? I dont wanna over-kill. – user2901683 Oct 31 '13 at 06:43
1

You can also use the Quartz.net scheduling service; in its simplest form it would install as a Windows Service and the schedule is configured in an XML file. You can then have it call your code at the scheduled time.

I kept some notes... not sure if this helps:


The Quartz.Net Guide I wish I had

Quartz.Net is very full featured, as far as I can read. If you use a scheduler so that you can defer tasks, or decouple a front-end from some rather long back-end processing, it can be done quite easily.

The large confusion for me was that Quartz can be used in many ways. You can embed it in your own app or service, etc. etc. The other confusion was that the official Tutorial could use some updating so that the code samples can work with the current version. Finally, the configuration seems pretty foreign for a .net user; I'm used to doing something like:

var myObject = new SuperUsefulClass();
myObject.ImportantSetting = SettingEnumeration.SomeValueThatIntellisenseSuggests;
myObject.OtherSetting = OtherSettingEnumeration.SomeValueThatIntellisenseSuggests;

Usually I like to explore a new class in LINQPad. I'll instantiate it as above, and play around with it until I am comfortable with using it in my project.

The configuration of Quartz.Net was about as foreign to me as imaginable. But that is because I overlooked one of the best pre-built options for using Quartz.Net, and I overlooked one of the best tutorials on Quartz.

For my use case, deferring tasks, I eventually realized the easiest way to use Quartz was with the pre-built Windows Service. To do that I simply modified an example configuration file, and I chose to persist all the jobs, job metadata, etc. to a SQL Server Database.

When it comes time for the job to be executed, the Quartz Windows Service will call the designated method (which of course implements the IJob interface). In order for the method to be called, your .dll or .exe ("assembly" in .net parlance) must be in the same folder where the service (Quartz.Service.exe) was installed. It then magically calls the method in your assembly. Of course if your method reads external files, those must also be copied to the service folder, or reachable somehow by the assembly.

After nearly giving up, I ran across Tarun Arora's tutorial. It is really excellent and since I cannot improve on it, I'll just link to it.

Install Quartz.Net as a windows service and Test installation

Quartz.Net Writing your first Hello World Job

Basically:

  1. Tweak the quartz.config file (similar to the code above)
  2. Install the Quartz.Net service (Quartz.Service.exe) as Administrator
  3. Put assemblies (.exe or .dll) of code you want to execute, in the same folder as Quartz.Service.exe
  4. Start the service
  5. Make scheduling calls. i.e. your assembly code will make calls to the Quartz Service, which then persists and executes them on its local system.
Colin
  • 1,987
  • 3
  • 17
  • 21
0

Create a service application: http://msdn.microsoft.com/en-us/library/zt39148a.aspx

ElGavilan
  • 6,610
  • 16
  • 27
  • 36