c# - C#为创建的每个任务使用取消令牌
问题描述
我有一个任务方法,可以在我的 MainWindow 中生成温度曲线,但是当用户将很多(生产的物品)的数据输入烤箱并每 30 秒记录一次温度时,它会以另一种形式调用。
public static async Task genLotProfile(string lotno,string filename, string datch,CancellationToken token)
try
{
//get temperature and do other stuff
Tprofile temp = getLogData(datch);
//task delay
while (!token.IsCancellationRequested)
{
//Task delay
await Task.Delay(30000, token);
}
}
catch (OperationCanceledException e)
{
}
我的想法是每次输入很多内容时都在输入表单中的输入按钮功能上调用它。
private void EnterBtn_Click(object sender, RoutedEventArgs e)
{
//do other stuff
//Call generate temp profile
string filename = MainWindow.getTempProfileName(MainWindow.datachannel, lotnoTBX.Text);
CancellationToken token = source.Token;
MainWindow.genLotProfile(lotnoTBX.Text, filename, MainWindow.datachannel, token);
}
我还有一个单独的取消课程
class Cancel
{
CancellationTokenSource _source;
public Cancel(CancellationTokenSource source)
{
_source = source;
}
public void cancelTask()
{
_source.Cancel();
}
}
问题是我将在我的条件下运行多个任务,并且当用户从烤箱中取出很多东西时,我想在另一个输出表单上使用 Cancel 类杀死特定任务,如果我这样做,取消令牌会杀死我正在运行的所有任务下列的
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
Cancel c = new Cancel(source);
c.cancelTask();
解决方案
您需要为每个任务创建新的 CancellationTokenSource:
private async void EnterBtn_Click(object sender, RoutedEventArgs e)
{
//do other stuff
//Call generate temp profile
string filename = MainWindow.getTempProfileName(MainWindow.datachannel, lotnoTBX.Text);
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
await MainWindow.genLotProfile(lotnoTBX.Text, filename, MainWindow.datachannel, token);
}
取消特定任务时,您只需调用正确的令牌实例 cancel()。
推荐阅读
- java - 如何将列表填写到我的 HashMap 中?
- wordpress - 当我使用动画时,Lite Speed Cache 导致网站上出现空白页面/空间
- haskell - 使 Applicative 成为 Monad 所需的“最低要求”是多少?
- r - 如何使用 quantmod 在同一个 DataFrame 中下载多个调整后的收盘价?
- python - 进行一项调查,告诉用户他们的大学专业匹配。使用 django 和 python
- javascript - 输入点后,event.target.value 在 input[type=number] 中变为空字符串
- typescript - 为什么自定义子查询类型不能分配给 GraphQLObjectType?
- c# - 是否可以在 ASP.NET MVC [FromBody] 控制器方法参数中使用多态性?
- css - 三幅图像下方有文字,但文字在较小屏幕上的图像旁边重新定位
- pandas - 基于 Python 中的日期偏移量更新列