我正在开发一个概念验证应用程序,它使用任务和信号量来计算数字列表,目前我有一个任务列表List<Task>
,它采用FactorNumberClass
然后计算因子当前正常工作的FactorNumberClass
中的具体数字。对于每个任务T,我有一个ContinueWith
任务,用于更新分解的总数的进度,平均分解时间,并更新进度条,其值为(数字成功分解)/(总数将为因式分解)。在对这些Tasks
进行分解时,请输入SemaphoreSlim.Wait(cancelToken)
,将当前分解限制为5个有效Tasks
。最后,我有一个ContinueWhenAll
,在所有任务完成后记录。假设没有取消,这一切都按照我的意图行事。
当我尝试取消任务时出现问题,我无法检测任务是否已被取消,因此无法准确确定该号码是否已成功分解或是否已被取消。如何检测父任务是否已取消或运行完成?
取消令牌定义:
public static CancellationTokenSource tokenSource = new CancellationTokenSource();
public static CancellationToken ct = tokenSource.Token;
因素类代码:
public class FactorNumberClass
{
public FactorNumberClass()
{
}
public FactorNumberClass(int num, int threadnum)
{
this.number = num;
this.threadNumber = threadnum;
}
public List<int> factors = new List<int>();
public int number;
public int max;
public int threadNumber;
}
保理方法:
public void Factor(FactorNumberClass F, CancellationToken token)
{
LogtoStatusText("Thread: " + F.threadNumber + " Trying to enter semaphore");
try
{
ASemaphore.Wait(ct);
F.max = (int)Math.Sqrt(F.number); //round down
for (int factor = 1; factor <= F.max; ++factor)
{ //test from 1 to the square root, or the int below it, inclusive.
if (F.number % factor == 0)
{
F.factors.Add(factor);
if (factor != F.number / factor)
{
F.factors.Add(F.number / factor);
}
}
}
F.factors.Sort();
Thread.Sleep(F.number * 300);
LogtoStatusText("Task: " + F.threadNumber + " Completed - Factors: " + string.Join(",", F.factors.ToArray()));
LogtoStatusText("Thread: " + F.threadNumber + " Releases semaphore with previous count: " + ASemaphore.Release());
}
catch (OperationCanceledException ex)
{
LogtoStatusText("Thread: " + F.threadNumber + " Cancelled.");
}
finally
{
}
}
开始处理的方法:
public void btnStart_Click(object sender, RoutedEventArgs e)
{
Task T;
List<Task> TaskList = new List<Task>();
LogtoStatusText("**** Begin creating tasks *****");
s1.Start();
AProject.FactorClassList.ForEach((f) =>
{
T = new Task(((x) => { OnUIThread(() => { RunningTasks++; }); Factor(f, ct); }), ct);
T.ContinueWith((y) =>
{
if (y.IsCompleted)
{
AProject.TotalProcessedAccounts++;
AProject.AverageProcessTime = (((Double)AProject.TotalProcessedAccounts / s1.ElapsedMilliseconds) * 1000);
}
OnUIThread(() => { RunningTasks--; });
OnUIThread(() => { UpdateCounts(AProject); });
});
TaskList.Add(T);
});
try
{
Task.Factory.ContinueWhenAll(TaskList.ToArray(), (z) => { LogtoStatusText("**** Completed all Tasks *****"); OnUIThread(() => { UpdateCounts(AProject); }); });
}
catch (AggregateException a)
{
// For demonstration purposes, show the OCE message.
foreach (var v in a.InnerExceptions)
LogtoStatusText("msg: " + v.Message);
}
LogtoStatusText("**** All tasks have been initialized, begin processing *****");
TaskList.ForEach(t => t.Start());
}
答案 0 :(得分:2)
释放finally
块中的信号量,以便始终正确释放。无需检测取消。
此外,隐藏在日志消息中的副作用也不是好的风格:
LogtoStatusText("..." + ASemaphore.Release());
我只是通过文字搜索找到了这个。从来没有注意到这个错误。
答案 1 :(得分:1)
使用取消令牌:
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main()
{
var tokenSource2 = new CancellationTokenSource();
CancellationToken ct = tokenSource2.Token;
var task = Task.Factory.StartNew(() =>
{
// Were we already canceled?
ct.ThrowIfCancellationRequested();
bool moreToDo = true;
while (moreToDo)
{
// Poll on this property if you have to do
// other cleanup before throwing.
if (ct.IsCancellationRequested)
{
// Clean up here, then...
ct.ThrowIfCancellationRequested();
}
}
}, tokenSource2.Token); // Pass same token to StartNew.
tokenSource2.Cancel();
// Just continue on this thread, or Wait/WaitAll with try-catch:
try
{
task.Wait();
}
catch (AggregateException e)
{
foreach (var v in e.InnerExceptions)
Console.WriteLine(e.Message + " " + v.Message);
}
finally
{
tokenSource2.Dispose();
}
Console.ReadKey();
}
}
https://msdn.microsoft.com/en-us/library/dd997396%28v=vs.110%29.aspx
答案 2 :(得分:0)
我终于找到了我正在寻找的解决方案,它允许我启动(Start()
)所有Task
个对象,通过semaphoreslim运行它们,观察CancellationToken
,以及然后检测Task
是否已取消或已正常完成。在这种情况下,Task
只能正常完成&#34;如果它已经进入信号量并在CancellationTokenSource.Cancel()
被解雇之前开始处理。
这个回答:Elegantly handle task cancellation把我推向了正确的方向。我最终抓住OperationCancelledException
,记录它,然后重新投掷它,在ContinueWith
Task
以下是解决了我的问题的更新代码
因素类:
private void Factor(FactorNumberClass F)
{
LogtoStatusText("Thread: " + F.threadNumber + " Trying to enter semaphore");
try
{
ASemaphore.Wait(ct);
F.max = (int)Math.Sqrt(F.number); //round down
for (int factor = 1; factor <= F.max; ++factor)
{ //test from 1 to the square root, or the int below it, inclusive.
if (F.number % factor == 0)
{
F.factors.Add(factor);
if (factor != F.number / factor)
{
F.factors.Add(F.number / factor);
}
}
}
F.factors.Sort();
Thread.Sleep(F.number * 300);
LogtoStatusText("Task: " + F.threadNumber + " Completed - Factors: " + string.Join(",", F.factors.ToArray()));
LogtoStatusText("Thread: " + F.threadNumber + " Releases semaphore with previous count: " + ASemaphore.Release());
}
catch
{
LogtoStatusText("Thread: " + F.threadNumber + " Cancelled");
throw;
}
finally
{
}
}
处理方法:
public void btnStart_Click(object sender, RoutedEventArgs e)
{
LaunchTasks();
}
private void LaunchTasks()
{
Task T;
List<Task> TaskList = new List<Task>();
LogtoStatusText("**** Begin creating tasks *****");
s1.Start();
AProject.FactorClassList.ForEach((f) =>
{
T = new Task(((x) => { OnUIThread(() => { RunningTasks++; }); Factor(f); }), ct);
T.ContinueWith((y) =>
{
if (y.Exception != null)
{
// LogtoStatusText(y.Status + " with "+y.Exception.InnerExceptions[0].GetType()+": "+ y.Exception.InnerExceptions[0].Message);
}
if (!y.IsFaulted)
{
AProject.TotalProcessedAccounts++;
AProject.AverageProcessTime = (((Double)AProject.TotalProcessedAccounts / s1.ElapsedMilliseconds) * 1000);
}
OnUIThread(() => { RunningTasks--; });
OnUIThread(() => { UpdateCounts(AProject); });
});
TaskList.Add(T);
});
try
{
Task.Factory.ContinueWhenAll(TaskList.ToArray(), (z) => { LogtoStatusText("**** Completed all Tasks *****"); OnUIThread(() => { UpdateCounts(AProject); }); });
}
catch (AggregateException a)
{
// For demonstration purposes, show the OCE message.
foreach (var v in a.InnerExceptions)
LogtoStatusText("msg: " + v.Message);
}
LogtoStatusText("**** All tasks have been initialized, begin processing *****");
TaskList.ForEach(t => t.Start());
}