Click here to Skip to main content
16,016,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to execute function every 5 min in windowform application and my question is which is recommended the timer or the coravel or hangfire ?

What I have tried:

timer
C#
private Timer timer1; 
public void InitTimer()
{
    timer1 = new Timer();
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = 300000; // in miliseconds
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    // all my methods 
}

Or
C#
scheduler.ScheduleAsync(async () =>
{
    await my method();
    //all my methods 
})
.EveryFiveMinute();
Posted
Updated 26-Jun-21 23:07pm
v2

Depends what you are doing every five minutes.
If you are using any UI controls, then the Timer is a much better fit because the Tick event happens on the UI thread - which means you don't have to invoke the control.
The down side of that is that the Tick event happens on the UI thread - so if what you are doing every five minutes takes significant time, then it will affect the user experience because your app will appear to "Freeze up" every five minutes.

If you use a separate thread, it can't access any UI controls directly, so you have to invoke such access back to the main UI thread or your app will fail.

In addition, the Timer control isn't "precise" - the Tick handler will be called after the Interval has elapsed, but it won't necessarily be called exactly on the five minutes - so if you need absolute precision then that's not going to work.

Really, there is "no best way" - all these things have advantages and disadvantages and you need to take the whole context in which they will be used into account.
 
Share this answer
 
<pre lang="C#">
using System.Reactive.Linq;

class cronJobCallingApi
    {
        IDisposable subscription = Observable.Interval(TimeSpan.FromMinutes(5.0)).SelectMany(_ => Observable.FromAsync(
         () => {
             createRoomReservation();
             ...
         }))
        .Subscribe();
 public static async Task createRoomReservation()
   {
   }

I have 4 error
#1 'Task<>' claims it is defined in 'System.Runtime',
#2 Reference to type 'CancellationToken' claims it is defined in 'System.Runtime', but it could not be found
#3 The type 'IAsyncOperation<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Windows.SDK.NET, Version=10.0.19041.10
#4 The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Windows.SDK.NET, Version=10.0.19041.10
 
Share this answer
 
Comments
Patrice T 27-Jun-21 6:16am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900