I have an application that will be running 24h / day the 7days / week, that is, it will always be running.
I need at a specific time, every day a method of this application is called.
I have an application that will be running 24h / day the 7days / week, that is, it will always be running.
I need at a specific time, every day a method of this application is called.
There is a great possibility that you just need to schedule a task on the operating system that calls something you need, eventually communicating with your application. In Windows, for example, you can use the Task Scheduler . p>
If you really want to do it within the application you can try doing everything at hand with the task scheduling class or with the Timer
class or better yet, use a library with Quartz . Other:
It has several implications for doing it on its own and most people do not understand all of them. Libraries help solve some but not all of them. Scheduling tasks within the application itself is often not a good idea unless you totally master the subject that is not simple. There are a lot of problems that need to be managed and what seemed simple becomes a huge difficulty.
I should note that @bigown's answer explains how to do it. I will just show a framework that I use for this and it caters to me. But since it is a third-party tool, it may not work someday.
To schedule methods in C#
, you can use Hangfire to do this. It can be installed via NuGet with the following command:
PM> Install-Package Hangfire
After this, simply configure the path to the database, where the tasks to be executed will be saved, like this:
GlobalConfiguration.Configuration
.UseColouredConsoleLogProvider()
.UseSqlServerStorage(@"Server=.\sqlexpress;Database=DataBaseName;Trusted_Connection=True;")
.UseMsmqQueues(@".\Private$\hangfire{0}", "default", "critical");
And to configure the methods, you can use Cron
or TimeSpan
. An example would look like this:
RecurringJob.AddOrUpdate(() => Console.WriteLine("Hello, world!"), Cron.Daily);
Where you change Console.WriteLine("Hello, world!")
by the method you want to call daily.
It works for Web or Desktop applications by changing only the configuration parameters.
Any detail, you can look the official documentation.
In a similar case, I used Azure's WebJob, where I needed to consume an API within a certain amount of time. Reference: link
Another alternative that can be used is the framework.
You can create a service.
Follow example
//definição da thread
private Thread _ThreadVerificacao;
public Backup()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//criação da thread de verificação e sua execução
_ThreadVerificacao = new Thread(VerificarHorario);
_ThreadVerificacao.Start();
}
//irá verificar se deve ou não executar o método a cada 1 hora
protected void VerificarHorario()
{
while (true)
{
if (DateTime.Now.Hour == 10) //Se for 10 horas da manhã
{
RealizaBackup();
}
Thread.Sleep(3600000); //3.600.000 milisegundos equivalem a 1 hora
}
}
protected void RealizaBackup()
{
//Aqui vai o método que realiza o backup
}