Len
Len

Reputation: 554

How to run different process using timer using c#

With the code below the processes runs every 1 minute

public partial class EmailService : ServiceBase
{
    private Timer timer = null;
    public EmailService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer = new Timer();
        this.timer.Interval = 60000;
        this.timer.Elapsed += new ElapsedEventHandler(this.timer_Tick);
        this.timer.Enabled = true;
        Library.WriteErrorLog("Notification Service started.");
    }
    private void timer_Tick(object sender, ElapsedEventArgs e)
    {
        try
        {
            NotificationManager.ProcessApprovalNotifications();
            NotificationManager.CreateRenewalNotifications();
            NotificationManager.ProcessRenewalNotifications();
        }
        catch (Exception ex)
        {
            Library.WriteErrorLog("FAC VMS Notification Service Error: " + ex.Source);
            Library.WriteErrorLog("FAC VMS Notification Service Error: " + ex.Message);
            Library.WriteErrorLog("FAC VMS Notification Service Error: " + ex.StackTrace);
        }
        Library.WriteErrorLog("FAC VMS Notification Service Run");
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
        Library.WriteErrorLog("Notification Service stopped.");
    }
}

How to make the processes run according to list below?

Upvotes: 0

Views: 480

Answers (2)

npo
npo

Reputation: 1060

I would suggest using a library that alreay does this kind of stuff maybe a scheduler, there are many in dotnet If yo uwant to build you own see this post https://codinginfinite.com/creating-scheduler-task-seconds-minutes-hours-days/

  using System;
using System.Collections.Generic;
using System.Threading;
public class SchedulerService
{
    private static SchedulerService _instance;
    private List<Timer> timers = new List<Timer>();
    private SchedulerService() { }
    public static SchedulerService Instance => _instance ?? (_instance = new SchedulerService());
    public void ScheduleTask(int hour, int min, double intervalInHour, Action task)
    {
        DateTime now = DateTime.Now;
        DateTime firstRun = new DateTime(now.Year, now.Month, now.Day, hour, min, 0, 0);
        if (now > firstRun)
        {
            firstRun = firstRun.AddDays(1);
        }
        TimeSpan timeToGo = firstRun - now;
        if (timeToGo <= TimeSpan.Zero)
        {
            timeToGo = TimeSpan.Zero;
        }
        var timer = new Timer(x =>
        {
            task.Invoke();
        }, null, timeToGo, TimeSpan.FromHours(intervalInHour));
        timers.Add(timer);
    }
}


using System;
public static class MyScheduler
{
    public static void IntervalInSeconds(int hour, int sec, double interval, Action task)
    {
        interval = interval/3600;
        SchedulerService.Instance.ScheduleTask(hour, sec, interval, task);
    }
    public static void IntervalInMinutes(int hour, int min, double interval, Action task)
    {
        interval = interval/60;
        SchedulerService.Instance.ScheduleTask(hour, min, interval, task);
    }
    public static void IntervalInHours(int hour, int min, double interval, Action task)
    {
        SchedulerService.Instance.ScheduleTask(hour, min, interval, task);
    }
    public static void IntervalInDays(int hour, int min, double interval, Action task)
    {
        interval = interval * 24;
        SchedulerService.Instance.ScheduleTask(hour, min, interval, task);
    }
}

Upvotes: 0

Klaus G&#252;tter
Klaus G&#252;tter

Reputation: 12032

You could remember the DateTime when NotificationManager.CreateRenewalNotifications was last called in a class field and only call it again if a day elapsed:

private Timer timer = null;
private DateTime lastCalledCreateRenewalNotifications = DateTime.MinValue;

NotificationManager.ProcessApprovalNotifications();
if (DateTime.Now - lastCalledCreateRenewalNotifications >= TimeSpan.FromDays(1))
{
    NotificationManager.CreateRenewalNotifications();
    lastCalledCreateRenewalNotifications = DateTime.Now;
}
NotificationManager.ProcessRenewalNotifications();

Upvotes: 2

Related Questions