【发布时间】:2021-07-24 14:08:24
【问题描述】:
我有两个 while 循环,我将与 TPL 并行运行。
我的代码:
public void Initialize()
{
cts = new CancellationTokenSource();
ParallelOptions options = new ParallelOptions();
options.CancellationToken = cts.Token;
options.MaxDegreeOfParallelism = Environment.ProcessorCount;
task = Task.Factory.StartNew(() => Parallel.Invoke(options, Watcher1, Watcher2), cts.Token);
}
public void Watcher1()
{
//Can I replace this (with a TPL construct in the initialize method)?
while(true)
{
//Do sth.
}
}
public void Watcher2()
{
//Can I replace this (with a TPL construct in the initialize method)?
while(true)
{
//do sth
}
}
如果我能安全地取消这两个动作就好了。能给我一些建议吗?
提前致谢。
亲切的问候,亲
【问题讨论】:
标签: c# .net-4.0 parallel-processing task-parallel-library