问题描述
大家好,我是多线程编码的新手,我的任务是更改当前工作的控制台应用程序的流程。当前,当控制台应用程序启动时,它会通过以下代码。 “Task.Factory.Startnew”方法启动新线程并运行“CheckDatabaseFieldStatus”,它正在检查数据库表字段的状态。如果此字段具有“取消”值,则它将调用 Token.cancel() 方法。
与此同时,当前线程正在执行某种逻辑并不断调用“CheckCancelTokenStatus”函数以抛出“ThrowIfCancellationRequested”异常。
新要求:我想从“Task.Factory.Startnew”方法创建的另一个线程中停止当前线程。如何以安全的方式从另一个线程中强制取消当前线程?
代码如下:
public CancellationTokenSource TokenSource = new CancellationTokenSource();
public CancellationToken Token = TokenSource.Token;
try
{
// Spin up a task that will keep checking the request to see if it has been cancelled.
Task cancelCheck = Task.Factory.StartNew(CheckDatabaseFieldStatus,TaskCreationoptions.LongRunning);
//Some logic to finish task
CheckCancelTokenStatus(Token);
//Some logic to finish task
CheckCancelTokenStatus(Token);
//Some logic to finish task
CheckCancelTokenStatus(Token);
//Some logic to finish task
}
catch (OperationCanceledException){
//Database call to Update status of task to canceled
}
//here is dispose method dispose token
这是函数“CheckDatabaseFieldStatus”
public void CheckDatabaseFieldStatus()
{
// While a Request is running,we're going to keep polling to check if we want to cancel.
while (!Token.IsCancellationRequested)
{
// Using the request timer here for our wait would mean that requests Could take up to 30
seconds to finish after they've actually finished.
Thread.Sleep(5000);
// Create a new context with each check to avoid conflicts with main context
using (DbContext mydb= new DbContext())
{
// Get the newest version of the request and check if it's set to cancel.
if (mydb.Table.GetAll().Any(r => r.Status=="cancelling"))
{
TokenSource.Cancel();
}
}
}
}
这里是函数 CheckCancelTokenStatus
function CheckCancelTokenStatus(CancellationToken Token)
{
if (Token.HasValue && Token.Value.IsCancellationRequested)
{
Token.Value.ThrowIfCancellationRequested();
}
}
解决方法
不要使用 Thread.Sleep
尝试使用 Task.Delay(int milliseconds,CancellationToken token)
。 Task.Delay 创建将在给定延迟后完成的任务。如果令牌在等待期间被取消,它将抛出异常并停止执行。最好在等待取消之前和之后进行检查,因为有人可能会在您睡觉时取消执行。
public void CheckDatabaseFieldStatus()
{
while (!Token.IsCancellationRequested)
{
Task.Delay(5000,Token).GetAwaiter().GetResult();
Token.ThrowIfCancellationRequested();
using (DbContext mydb= new DbContext())
{
if (mydb.Table.GetAll().Any(r => r.Status=="cancelling"))
{
TokenSource.Cancel();
}
}
}
}
此外,我使用 GetAwaiter().GetResult()
而不是简单的 Wait()
因为第二个选项包装异常。请参阅此答案以获取解释https://stackoverflow.com/a/38530225/10339675
一般来说最好使用 async
/await
而不是同步等待