Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows service scheduling to run daily once a day at 6:00 AM

I had created a windows service and i want that the service will Schedule to run daily at 6:00 Am. Below is the code which i had written:-

public Service1()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    try
    {
        ExtractDataFromSharePoint();
    }
    catch (Exception ex)
    {
        //Displays and Logs Message
        _loggerDetails.LogMessage = ex.ToString();
        _writeLog.LogDetails(_loggerDetails.LogLevel_Error, _loggerDetails.LogMessage);
    }
}

In the above code you can see that in OnStart Method of service i am calling a Function ExtractDataFromSharePoint(). How i will schedule this to run daily morning at 6:00 AM.

like image 763
Dharmendra Kumar Singh Avatar asked May 29 '14 05:05

Dharmendra Kumar Singh


People also ask

How do I schedule a Windows service to run daily?

Open Task Scheduler (Start > in search type Task Scheduler and select when found). Once Task Scheduler opens, in the right column window click on Create Task... In the General tab, type a name for the service. Enable the "Run whether user is logged on or not" and "Run with highest privileges".

How do I run a Windows service continuously in C#?

Inside the loop, we sleep for 1 second. You'll want to replace this with the work you need to do - monitor proxy settings, etc. Finally, in the OnStop() callback of your Windows Service, you want to signal the thread to stop running. This is easy using the _shutdownEvent .


2 Answers

Here, you have 2 ways to execute your application to run at 6 AM daily.

1) Create a console application and through windows scheduler execute on 6 AM.

2) Create a timer (System.Timers.Timer) in your windows service which executes on every defined interval and in your function, you have to check if the system time = 6 AM then execute your code

ServiceTimer = new System.Timers.Timer();
ServiceTimer.Enabled = true;
ServiceTimer.Interval = 60000 * Interval;
ServiceTimer.Elapsed += new System.Timers.ElapsedEventHandler(your function);

Note: In your function you have to write the code to execute your method on 6 AM only not every time

like image 159
Rachit Patel Avatar answered Sep 18 '22 17:09

Rachit Patel


You don't need a service for this. Just create a regular console app, then use the Windows scheduler to run your program at 6am. A service is when you need your program to run all the time.

like image 44
David Avatar answered Sep 19 '22 17:09

David