11

How do I write a Alert which will run at 00:00, 00:15, 00:30, and so on every day?

Can you give me an example code?

Thank you.

Ricky
  • 34,377
  • 39
  • 91
  • 131
  • Are you talking about writing a windows service which runs every 15 mins? – K Mehta Oct 14 '11 at 08:14
  • 2
    Normally if you really need to do it, you do it with Quartz.NET . If you don't really need it to be "resistant" you can do quite easily with .NET timers. – xanatos Oct 14 '11 at 08:16
  • 1
    A standard sheduled task, that runs an executable, is often a good alternative because of its simplicity to create, and the wide panel of configuration options you can give to your admins (identity, fine grained schedule,...) – Steve B Oct 14 '11 at 08:18

6 Answers6

12

You can use a timer that runs every minute that checks the current time. The check can look like:

private void OnTimerTick()
{
    if(DateTime.Now.Minutes%15==0)
    {
        //Do something
    }
}

But if you are looking for a program or service that has to be run every 15 minutes you can just write your application and have it started using a windows planned task.

PVitt
  • 11,500
  • 5
  • 51
  • 85
11

You can write your program to execute certain task every time it is opened then you use Windows Scheduled Tasks to execute your program every day at certain times.

your program will be simpler and you will focus only on the logic you need to implement, the scheduling will be done by Windows for you, for free (assuming you already paid Windows :) ).

if you really want to implement the scheduling by yourself, there are frameworks and libraries like this one: Quartz.NET

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • 1
    This is what I generally recommend, too. Use the task scheduler, that is what it is there for. – Harry Steinhilber Oct 14 '11 at 13:14
  • 1
    I found Quartz.NET a viable option, however it 1) tightly couples its `IJob` interface to quartz.dll and 2) requires every single data to be serializable. Here is a barebone version: https://github.com/mjeson/SimpleJobScheduler – Jeson Martajaya Feb 22 '18 at 18:25
3

Here is a basic C# scheduler:

using System;
using System.Threading;
using System.Windows.Forms;
using System.IO;

public class TimerExample
{
    public static void Main()
    {
        bool jobIsEnabledA = true;
        bool jobIsEnabledB = true;
        bool jobIsEnabledC = true;

        Console.WriteLine("Starting at: {0}", DateTime.Now.ToString("h:mm:ss"));

        try
        {
            using (StreamWriter writer = File.AppendText("C:\\scheduler_log.txt"))
            {
                while (true)
                {
                    var currentTime = DateTime.Now.ToString("h:mm");

                    if (currentTime == "3:15" && jobIsEnabledA)
                    {
                        jobIsEnabledA = false;
                        ThreadPool.QueueUserWorkItem((state) => { MessageBox.Show(string.Format("Time to brush your teeth! {0}", currentTime) ); });
                    }

                    if (currentTime == "3:20" && jobIsEnabledB)
                    {
                        jobIsEnabledB = false;
                        ThreadPool.QueueUserWorkItem((state) => { MessageBox.Show(string.Format("Time to brush your teeth! {0}", currentTime)); });
                    }

                    if (currentTime == "3:30" && jobIsEnabledC)
                    {      
                        jobIsEnabledC = false;
                        ThreadPool.QueueUserWorkItem((state) => { MessageBox.Show(string.Format("Time for your favorite show! {0}", currentTime)); });
                    }

                    if (currentTime == "3:31")
                    {      
                        jobIsEnabledA = true;
                        jobIsEnabledB = true;
                        jobIsEnabledC = true;
                    }

                    var logText = string.Format("{0} jobIsEnabledA: {1} jobIsEnabledB: {2} jobIsEnabledC: {3}", DateTime.Now.ToString("h:mm:ss"), jobIsEnabledA, jobIsEnabledB, jobIsEnabledC);
                    writer.WriteLine(logText);

                    Thread.Sleep(1000);
                }
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    }
}
sp2012
  • 133
  • 8
  • I wonder if for some reason the machine is busy on other threads and skips the job's time. I know is not likely but is there a chance that the thread will not resume instantly after the Thread.Sleep(1000)? – qfactor77 Feb 12 '23 at 18:01
2

If you want the windows service that executes some code with some time interwal, you need to create the service project (see here), and use in this service the Timer class (see here). If you want to run these task when your windows application is executes, use the Windows.Forms.Timer, as mentioned before.

Alex_L
  • 2,658
  • 1
  • 15
  • 13
1

There are different timers in .NET

  • System.Threading.Timer
  • System.Windows.Forms.Timer
  • System.Timers.Timer

Depending on what you want to do (and in which environment, threadsave, bla bla) you should choose one of them.

some more information

fixagon
  • 5,506
  • 22
  • 26
1

For example:

using System.Timers;
    class Program
    {
        static void Main(string[] args)
        {
            Timer timer = new Timer();
            timer.Interval = new TimeSpan(0, 15, 0).TotalMilliseconds;
            timer.AutoReset = true;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
        }

        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
mike
  • 550
  • 2
  • 9
  • 24