并行执行永无止境的多个任务
Execute never ending multIple Task in parallel
我正在处理控制台 EXE,我必须连续下载特定数据,对其进行处理并将其结果保存在 MSSQL DB 中。
我参考 Never ending Task 创建单个任务,它适用于我的一种方法。
我有 3 个方法可以同时执行,所以我创建了 3 个我想连续并行执行的任务,所以我的代码在这里做了一些改动
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();
我想并行执行方法一、方法二和方法三。但是当我测试它时只有方法3在执行
我搜索了替代方法,但没有找到合适的指导。有什么合适有效的方法吗
没有必要使用 Parallel.ForEach 因为你已经有 3 个任务。应该这样做:
var actions = new Action[] { EventCallBack, LogCallBack, DataCallBack };
await Task.WhenAll(actions.Select(async action =>
{
while (!_cts.Token.IsCancellationRequested)
{
action();
ExecutionCore(_cts.Token);
await Task.Delay(ExecutionLoopDelayMs, _cts.Token)
}
}, _cts.Token));
我正在处理控制台 EXE,我必须连续下载特定数据,对其进行处理并将其结果保存在 MSSQL DB 中。
我参考 Never ending Task 创建单个任务,它适用于我的一种方法。 我有 3 个方法可以同时执行,所以我创建了 3 个我想连续并行执行的任务,所以我的代码在这里做了一些改动
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();
我想并行执行方法一、方法二和方法三。但是当我测试它时只有方法3在执行
我搜索了替代方法,但没有找到合适的指导。有什么合适有效的方法吗
没有必要使用 Parallel.ForEach 因为你已经有 3 个任务。应该这样做:
var actions = new Action[] { EventCallBack, LogCallBack, DataCallBack };
await Task.WhenAll(actions.Select(async action =>
{
while (!_cts.Token.IsCancellationRequested)
{
action();
ExecutionCore(_cts.Token);
await Task.Delay(ExecutionLoopDelayMs, _cts.Token)
}
}, _cts.Token));