Reputation: 213
The requirement is to call a method after every 12 hours. The code below the method call should keep on running in different thread, how can we achieve this?
void ExecuteAfterTimeInterval()
{
//some code
}
public static void main(string[] args)
{
//call the below method after every 12 hours
ExecuteAfterTimeInterval();
// run the below code in separate thread
// some code here
//some code here
//some code here
}
Upvotes: 2
Views: 1890
Reputation: 751
Give this a try. Look for //SET BREAK POINT HERE and run.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace MyConsole
{
class Program
{
static void Main(string[] args)
{
Program console = new Program();
console.MyMethodAsync();
}
void ExecuteAfterTimeInterval()
{
//some code
}
public async Task MyMethodAsync()
{
Task<int> longRunningTask = LongRunningOperationAsync();
// run the below code in separate thread
//some code here
//some code here
for (int i = 0; i < 10000000000; i++)
{
Console.WriteLine(i); //SET BREAK POINT HERE
}
//some code here
//and now we call await on the task
int result = await longRunningTask;
}
public async Task<int> LongRunningOperationAsync() // assume we return an int from this long running operation
{
bool retry = true;
using (AutoResetEvent wait = new AutoResetEvent(false))
{
while (retry)
{
//Do Work here
//await Task.Delay(43200000); //12 hour delay
await Task.Delay(3000); //SET BREAK POINT HERE
}
}
return 1;
}
}
}
Upvotes: 1