3

how to make windows service to run on every monday at 12 : 00 AM

please guide me .

i have tried following one..

 protected void InitializeTimer()
    {            
        if (timer1 == null)
        {
            timer1 = new Timer();
            timer1.AutoReset = true;
            var todayInWeek = DateTime.Now.Date.DayOfWeek;
            DateTime _scheduleTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
            if (todayInWeek == DayOfWeek.Monday)
            {
                timer1.Elapsed += new ElapsedEventHandler(schedular());
            }

        }           
    }

    private void schedular(object sender, ElapsedEventArgs e)
    {
        try
        {

        }
        catch (Exception ex)
        {

        }
    }
DotNetLover
  • 229
  • 1
  • 4
  • 11
  • 1
    You can use task scheduler instead. Windows service is usually helpful when the interval is smaller - in minutes perhaps. – Nisarg Shah Sep 15 '17 at 06:03
  • Thank you for giving me response i will go through task scheduler ...can you provide me some good references for this ....task scheduler – DotNetLover Sep 15 '17 at 06:09
  • May be this link will help you. https://stackoverflow.com/questions/23926141/windows-service-scheduling-to-run-daily-once-a-day-at-600-am – kritikaTalwar Sep 15 '17 at 06:20
  • The Task Scheduler has nothing to do with your VB.NET application. You simply create a Console app like any other. Task Scheduler is a Windows utility that you invoke in Windows and create a task that will invoke your application. – jmcilhinney Sep 15 '17 at 06:23
  • As per my thinking, Windows services is something that can be invoke once and it should running always. For this we make code like never ending loop with specific condition. Condition like in your case, you can filtered that further processing can only be started when its Monday 12 AM. While(true){ if(DateTime.Now.ToString("MMM") == "Monday" && DateTime.Now.ToString("HH:mm:ss tt") == "12:00:00 AM") { // Further Processing } } – Mohd Ismail Siddiqui Sep 15 '17 at 06:39

2 Answers2

0

As people have said in comments, create a simple console application and then schedule a task.

To start Task Scheduler simply type Task Scheduler or Taskschd.msc in Windows search bar.

Then select a Create Task... on the right hand side. Point it to your console application and then create a new trigger that says it should run every monday at 12.00 like this:

enter image description here

For more information in creating tasks in Task Scheduler

https://technet.microsoft.com/en-us/library/cc748993%28v=ws.11%29.aspx?f=255&MSPPError=-2147217396

Ogglas
  • 62,132
  • 37
  • 328
  • 418
0

You could look at using Topshelf and Quartz.net for scheduling - you could create a console application which gets installed as a windows service and then configure it to run every 24 hours. It would then be very flexible if you ever needed to change the scheduling - I find scheduling tasks through the windows GUI kind of clunky.

thisextendsthat
  • 1,266
  • 1
  • 9
  • 26