7
I have an application that will run 24h/day the 7 days/week, IE, It will always be running.
I need that at a specific time, every day a method of this application is called.
7
I have an application that will run 24h/day the 7 days/week, IE, It will always be running.
I need that at a specific time, every day a method of this application is called.
17
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 Task Scheduler.
If you really want to do inside the application you can try to do everything by hand with the task scheduling class or with the class Timer
or better still, use a library with the Quartz. Other:
It has several implications to do on your own and most people don’t understand them all. Libraries help solve some but not all of them. Scheduling tasks within the application itself is not usually a good idea unless you fully master the issue that is not simple. There are a lot of problems that need to be managed and what seemed simple becomes a huge difficulty.
Chroniton has been improved. It also now Supports . NET Core
5
I must point out that the @Maniero response explains how to do it. I will just show a framework what use for it and suits me. But as it is a third party tool, it may not suit some day.
To schedule methods in C#
, you can use the Hangfire for this. It can be installed via Nuget with the following command:
PM> Install-Package Hangfire
After that, just set the path to the database, where tasks will be saved to be executed, this way:
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 do you change the Console.WriteLine("Hello, world!")
by the method you wish to be called daily.
It works for Web or Desktop applications by changing only the configuration parameters.
Any detail you can look at the official documentation.
1
In a similar case I used Azure Webjob, where I needed to consume an API in a certain time interval. Reference: https://docs.microsoft.com/pt-br/azure/app-service/web-sites-create-web-jobs
-1
You can create a service.
http://www.devmedia.com.br/criando-um-servico-utilizando-c/22912 https://guilerme18.wordpress.com/2014/01/28/windowsservicesvs2012/
Following 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
}
This is a simplistic solution covered with problems.
If you don’t understand ask. This is just a simple example for you to adapt your need.
Browser other questions tagged c# .net scheduling
You are not signed in. Login or sign up in order to post.
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero