Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update method time in HangFire RecurringJob?

Tags:

c#

hangfire

I just discovered that with HangFire you can have background jobs running in your .Net application but when I was testing its functionalities, I realized that the time in the method I added to a 1 minute RecurringJob does not change even though the method was triggered multiple times:

Console Application

Method:

private static string GetTime()
{
      return DateTime.Now.ToLongTimeString();
}

RecurringJob statement:

 RecurringJob.AddOrUpdate("time", () => Console.WriteLine("Hello Hangfire! " + GetTime()), "*/1 * * * *");

My question:

Is it possible to display time updates as RecurringJob calls the method above?

like image 424
Oluwafemi Avatar asked Jun 30 '15 14:06

Oluwafemi


1 Answers

the string you pass to console.writeline is being serilized and that's why it gets the time when it the anonymous method is added, bug or not? well i guess it depends on what you expect from it to do. it is explained in the documentation Hangfire Passing arguments

try using a method like this instead

private static void HelloWorldTime()
{
   Console.Writeline( "hello world! : " + DateTime.Now.ToLongTimeString() );
}

RecurringJob.AddOrUpdate("time", HelloWorldTime() , "*/1 * * * *");
like image 146
Thorarins Avatar answered Sep 23 '22 05:09

Thorarins