Calling a Method at Specific Time Every Day

How to call a method daily, at specific time, in C#?

  • Create a console app that does what you're looking for
  • Use the Windows "Scheduled Tasks" functionality to have that console app executed at the time you need it to run

That's really all you need!

Update: if you want to do this inside your app, you have several options:

  • in a Windows Forms app, you could tap into the Application.Idle event and check to see whether you've reached the time in the day to call your method. This method is only called when your app isn't busy with other stuff. A quick check to see if your target time has been reached shouldn't put too much stress on your app, I think...
  • in a ASP.NET web app, there are methods to "simulate" sending out scheduled events - check out this CodeProject article
  • and of course, you can also just simply "roll your own" in any .NET app - check out this CodeProject article for a sample implementation

Update #2: if you want to check every 60 minutes, you could create a timer that wakes up every 60 minutes and if the time is up, it calls the method.

Something like this:

using System.Timers;

const double interval60Minutes = 60 * 60 * 1000; // milliseconds to one hour

Timer checkForTime = new Timer(interval60Minutes);
checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
checkForTime.Enabled = true;

and then in your event handler:

void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
if (timeIsReady())
{
SendEmail();
}
}

Running a method every day at a specific time with Timer()

val timer = Timer()
val task: TimerTask = object : TimerTask() {
override fun run() {
// do your task here
}
}
// repeat every hour
timer.schedule(task, 0L, 1000 * 60 * 60)

As refer to here.

Execute a method once a day, at a specific time, on a running server in .NET Core

There are a couple of options that you should look into.

  • Native .NET Core solution through IHostedService
  • Hangfire
    • Personally, I like that you can configure a dashboard and see what happened during the execution of your scheduled tasks.
  • As suggested by @Lei Yang in the comments, Quartz.NET

Hangfire is still adding in-memory support. Therefore, you're required to have a SQL Server to store all the information about the job (triggers, states, etc.).

This can have a huge impact in your decision. On the other hand, Quartz.NET does support in-memory store type.

How to call a method on specific time in java?

Using a java.util.Timer class you can create a timer and schedule it to run at specific time.

Below is the example:

//The task which you want to execute
private static class MyTimeTask extends TimerTask
{

public void run()
{
//write your code here
}
}

public static void main(String[] args) {

//the Date and time at which you want to execute
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormatter .parse("2012-07-06 13:05:45");

//Now create the time and schedule it
Timer timer = new Timer();

//Use this if you want to execute it once
timer.schedule(new MyTimeTask(), date);

//Use this if you want to execute it repeatedly
//int period = 10000;//10secs
//timer.schedule(new MyTimeTask(), date, period );
}


Related Topics



Leave a reply



Submit