Reputation: 522
I have a recurring job that is called on Startup. It should call PlaceOrder() every 20 seconds.
Job looks like this
RecurringJob.AddOrUpdate<MethodCaller>(a => a.PlaceOrder(), "*/20 * * * * *");
PlaceOrder() method calls RevokeOrder() after 5 seconds delay.
Methods
public void PlaceOrder()
{
DateTimeOffset d = DateTimeOffset.Now;
Debug.WriteLine("PLACED : {0}", d);
Task.Delay(5000).ContinueWith(t => RevokeOrder());
}
public void RevokeOrder()
{
DateTimeOffset d = DateTimeOffset.Now;
Debug.WriteLine("REVOKED : {0}",d);
}
Problem is that both methods are called in every 15 seconds or sometimes in every 30 seconds instead of 20.
Logs :
PLACED : 1:59:27
REVOKED : 1:59:32
PLACED : 1:59:42
REVOKED : 1:59:47
PLACED : 2:00:12
REVOKED : 2:00:17
PLACED : 2:00:27
REVOKED : 2:00:32
PLACED : 2:00:43
REVOKED : 2:00:48
I want PlaceOrder() and RevokeOrder() methods to be called every 20 seconds. But there should be 5 seconds delay between them. How do I fix it?
Upvotes: 0
Views: 281
Reputation: 987
See below a working Console Application that resolves the issue. Uses a timer instead of the recurringJob.
class Program
{
static void Main(string[] args)
{
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromSeconds(20);
var timer = new System.Threading.Timer(async (e) =>
{
await PlaceOrderAsync();
}, null, startTimeSpan, periodTimeSpan);
Console.ReadLine();
}
private static async Task PlaceOrderAsync()
{
DateTimeOffset d = DateTimeOffset.Now;
Console.WriteLine("PLACED : {0}", d);
await Task.Delay(5000).ContinueWith(t => RevokeOrder());
}
public static void RevokeOrder()
{
DateTimeOffset d = DateTimeOffset.Now;
Console.WriteLine("REVOKED : {0}", d);
}
}
Upvotes: 1