I'm working on console EXE where I have to download particular data continuously, process on it and save its result in MSSQL DB.
I refer Never ending Task for single Task creation and it works for me for one method. I have 3 methods to execute simultaneously so I created 3 Task which I want to execute parallel continuously, so made few changes in code here is my code
CancellationTokenSource _cts = new CancellationTokenSource();
var parallelTask = new List<Task>
{
new Task(
() =>
{
while (!_cts.Token.WaitHandle.WaitOne(ExecutionLoopDelayMs))
{
DataCallBack(); // method 1
ExecutionCore(_cts.Token);
}
_cts.Token.ThrowIfCancellationRequested();
},
_cts.Token,
TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning),
new Task(
() =>
{
while (!_cts.Token.WaitHandle.WaitOne(ExecutionLoopDelayMs))
{
EventCallBack(); // method 2
ExecutionCore(_cts.Token);
}
_cts.Token.ThrowIfCancellationRequested();
},
_cts.Token,
TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning),
new Task(
() =>
{
while (!_cts.Token.WaitHandle.WaitOne(ExecutionLoopDelayMs))
{
LogCallBack(); //method 3
ExecutionCore(_cts.Token);
}
_cts.Token.ThrowIfCancellationRequested();
},
_cts.Token,
TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning)
};
Parallel.ForEach(parallelTask, task =>
{
task.Start();
task.ContinueWith(x =>
{
Trace.TraceError(x.Exception.InnerException.Message);
Logger.Logs("Error: " + x.Exception.InnerException.Message);
Console.WriteLine("Error: " + x.Exception.InnerException.Message);
}, TaskContinuationOptions.OnlyOnFaulted);
});
Console.ReadLine();
I want to execute method 1, method 2 and method 3 parallel. But when I tested it only method3 is executing
I searched for alternate but did not found suitable guidance. is there any proper efficient way to do it.