请停止正在进行的任务 [英] HOw to stop a Task which is in process

查看:95
本文介绍了请停止正在进行的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我在列表框中有一个项目列表。

I have a list of items in a list box.

当用户选择一个项目时,我启动  a处理更新其他列表框的长操作的任务

When user select a single Item, I start  a Task to process a long operation which update an other listbox

我的任务只是一个方法,在项目选择更改时调用如下:

My task is simply a method which is called on item selection change as below :

	Task.Run(() => FetchSparkData(_channels, _phases));

如果我的用户选择项目并等待任务,这非常有用在选择其他项目之前完成。

This works great if my user select an items and wait for the Task to complete before selecting an other items.

但是发生的事情是,如果我的用户选择项目,然后在任务运行时立即选择另一项,则处理2项而不是已被选中。

But what is happening is that if my user select items and then select an other right away while the Task is running it process 2 items instead of the one which has been selected.

如果我的用户在当前项目正在进行中选择了其他项目时,中止任务的方法是什么?

What is the way to abort a Task if my user selected an other items while the current one is in process ?

问候

推荐答案

你好,

 阅读以下链接中的信息;

 Read the information at the following link;

https://docs.microsoft。 com / zh-cn / dotnet / standard / threading / cancellation-in-managed-threads

https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads

 

 应告知您项目需要了解的内容。

 Should be inform you of what you need to know for your project.

 

 谢谢


这篇关于请停止正在进行的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆