how to execute task in a certain time?

mc 5,491 Reputation points
2023-06-06T00:33:28.79+00:00

If I want to execute something in a certain time how to do it? except timer?

If I want to update the queue at 09:56 how to do it?

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-06-06T10:49:28.1166667+00:00

    some code

    Seriously!? You ask for help but can't be bothered to clarify? Every OS comes with a scheduler and hosting services have schedulers.

    Windows comes with Task Scheduler which can execute, command line, batch scripts, executables, powers shell, etc.

    https://www.windowscentral.com/how-create-automated-task-using-task-scheduler-windows-10

    SQL Server has SQL Agent which can create scheduled jobs for database tasks like stored procedures, scripts, SSIS packages, etc.

    Create a Job

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Chen Li - MSFT 1,231 Reputation points
    2023-06-06T06:04:25.6066667+00:00

    Hi @mc,

    You can use Hangfire to complete, you need to configure the corresponding cron expressions to set the time.

    For example,

    Firstly, you need to install the corresponding Nuget Packages: Hangfire, Microsoft.Data.SqlClient:

    User's image

    Then configure Hangfire in Program.cs:

    //...
    builder.Services.AddHangfire(x => x.UseSqlServerStorage("Server=(localdb)\\mssqllocaldb;Database=ProCodeGuide.Samples.Hangfire;Trusted_Connection=True;MultipleActiveResultSets=true"));
    builder.Services.AddHangfireServer();
    //...
    app.UseHangfireDashboard();
    app.UseHangfireServer();
    //add your job, you can also add this in other class, for example: Controller
    RecurringJob.AddOrUpdate(() => Console.WriteLine("This is my test job"), "55 13 * * *", TimeZoneInfo.Local);
    //...
    

    You need to create your connection database and the table structure will be added automatically when running.

    User's image

    Note: If you are implementing a non-static method, please refer to the way in this GitHub link.

    Test Result:

    User's image

    You can access the dashboard by typing /hangfire after your url:

    User's image

    Of course, you can also choose other ways to complete:

    1. Native .NET Core solution through IHostedService.
    2. Quartz.NET.
    3. Azure Queue Storage, and here is a case about this: schedule a background task (Cron Jobs) with hosted services for a specific date and time in the future.

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Chen Li


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.