Schedule execution process in C #

7

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.

    
asked by anonymous 17.08.2015 / 15:40

5 answers

14

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.

    
17.08.2015 / 15:52
4
  

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.

    
15.06.2016 / 14:56
1

In a similar case, I used Azure's WebJob, where I needed to consume an API within a certain amount of time. Reference: link

  • I have implemented an application in console application to consume the api and perform the tasks that would be necessary, in my case send an email.
  • I compiled the project, compacted the bin / Release file of consoleApp in .ZIP format.
  • I created a WebJob in my project published in Azure.
  • During the configuration I used a CRON expression to execute my consoleApp every x time. link
  • At last, just start the webJob. Ready. Very simple and has log. Hope this helps.
29.01.2018 / 18:11
0

Another alternative that can be used is the framework.

Quartz.NET - Quartz Enterprise Scheduler .NET

    
29.01.2018 / 20:08
-1

You can create a service.

link link

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
        }
    
18.08.2015 / 19:19