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.
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".
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 .
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With