Home > Net >  how to manage Timer with Interval
how to manage Timer with Interval

Time:04-28

I need to send a report to every month first day at 6am. Please help for the logic for this for time interval for window service, please suggest

private System.Timers.Timer svcTimerForSendEmailSrvc = null;

protected override void OnStart(string[] args)
    {
            svcTimerForSendEmailSrvc = new System.Timers.Timer();
            svcTimerForSendEmailSrvc.Enabled = true;
            svcTimerForSendEmailSrvc.Interval = //here not getting time every month 1st date 6am mail should send
            svcTimerForSendEmailSrvc.Elapsed  = new System.Timers.ElapsedEventHandler(svcTimerForSendEmailSrvc_Elapsed);
            svcTimerForSendEmailSrvc.Start();
    }

void svcTimerForSendEmailSrvc_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        SendReportEverymonth1day();
    }

Example i need send email to every month 1 6am.

like 1 jan 2022 next mail 1 feb 2022 next....1 dec 2022

CodePudding user response:

Third-party solutions such as FluentScheduler or Quartz.NET can be used to schedule code to run on a specific interval, e.g.:

Schedule<MyComplexJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);

A timer cannot.

Another option, as suggested by @Fildor, is to use the Task Scheduler in Windows to start a program that runs your code on a given schedule like for example once a month.

  • Related