繁体   English   中英

如果正在运行则取消异步任务

[英]cancel async task if running

我有几种方法调用以下方法(例如文本框的onkeyup),它异步过滤列表框中的项目。

private async void filterCats(string category,bool deselect)
{

    List<Category> tempList = new List<Category>();
    //Wait for categories
    var tokenSource = new CancellationTokenSource();
    var token = tokenSource.Token;

    //HERE,CANCEL TASK IF ALREADY RUNNING


    tempList= await _filterCats(category,token);
    //Show results
    CAT_lb_Cats.DataSource = tempList;
    CAT_lb_Cats.DisplayMember = "strCategory";
    CAT_lb_Cats.ValueMember = "idCategory";

}

以及以下任务

private async Task<List<Category>> _filterCats(string category,CancellationToken token)
{
    List<Category> result = await Task.Run(() =>
    {
        return getCatsByStr(category);
    },token);

    return result;
}

我想测试任务是否已经运行,如果是,取消它并使用新值启动它。 我知道如何取消任务,但是如何检查它是否已经在运行?

这是我用来执行此操作的代码:

if (_tokenSource != null)
{
    _tokenSource.Cancel();
}

_tokenSource = new CancellationTokenSource();

try
{
    await loadPrestatieAsync(_bedrijfid, _projectid, _medewerkerid, _prestatieid, _startDate, _endDate, _tokenSource.Token);
}
catch (OperationCanceledException ex)
{
}

对于程序调用它是这样的(当然简化):

private async Task loadPrestatieAsync(int bedrijfId, int projectid, int medewerkerid, int prestatieid,
        DateTime? startDate, DateTime? endDate, CancellationToken token)
{
    await Task.Delay(100, token).ConfigureAwait(true);
    try{
        //do stuff

        token.ThrowIfCancellationRequested();

    }
    catch (OperationCanceledException ex)
    {
       throw;
    }
    catch (Exception Ex)
    {
        throw;
    }
}

我正在做100毫秒的延迟,因为相同的动作被相当快速和重复地触发,100毫秒的小推迟使得它看起来实际上更具响应性。

您似乎正在寻找一种方法来从文本框中输入的文本中获取“自动完成列表”,其中当搜索开始后文本发生更改时,将取消正在进行的异步搜索。

正如评论中提到的,Rx(Reactive Extensions)为此提供了非常好的模式,允许您轻松地将UI元素连接到可取消的异步任务,构建重试逻辑等。

下面不到90行的程序显示了一个“完整的UI”样本(不幸的是不包括任何猫;-)。 它包括一些关于搜索状态的报告。

简单的UI

我在RxAutoComplete类中使用了许多静态方法创建了这个,以显示如何通过小的文档化步骤实现这一点,以及如何将它们组合起来,以实现更复杂的任务。

namespace TryOuts
{
    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Reactive.Linq;
    using System.Threading;

    // Simulated async search service, that can fail.
    public class FakeWordSearchService
    {
        private static Random _rnd = new Random();
        private static string[] _allWords = new[] {
            "gideon", "gabby", "joan", "jessica", "bob", "bill", "sam", "johann"
        };

        public async Task<string[]> Search(string searchTerm, CancellationToken cancelToken)
        {
            await Task.Delay(_rnd.Next(600), cancelToken); // simulate async call.
            if ((_rnd.Next() % 5) == 0) // every 5 times, we will cause a search failure
                throw new Exception(string.Format("Search for '{0}' failed on purpose", searchTerm));
            return _allWords.Where(w => w.StartsWith(searchTerm)).ToArray();
        }
    }

    public static class RxAutoComplete
    {
        // Returns an observable that pushes the 'txt' TextBox text when it has changed.
        static IObservable<string> TextChanged(TextBox txt)
        {
            return from evt in Observable.FromEventPattern<EventHandler, EventArgs>(
                h => txt.TextChanged += h,
                h => txt.TextChanged -= h)
                select ((TextBox)evt.Sender).Text.Trim();
        }

        // Throttles the source.
        static IObservable<string> ThrottleInput(IObservable<string> source, int minTextLength, TimeSpan throttle)
        {
            return source
                .Where(t => t.Length >= minTextLength) // Wait until we have at least 'minTextLength' characters
                .Throttle(throttle)      // We don't start when the user is still typing
                .DistinctUntilChanged(); // We only fire, if after throttling the text is different from before.
        }

        // Provides search results and performs asynchronous, 
        // cancellable search with automatic retries on errors
        static IObservable<string[]> PerformSearch(IObservable<string> source, FakeWordSearchService searchService)
        {
            return from term in source // term from throttled input
                   from result in Observable.FromAsync(async token => await searchService.Search(term, token))
                        .Retry(3)          // Perform up to 3 tries on failure
                        .TakeUntil(source) // Cancel pending request if new search request was made.
                   select result;
        }

        // Putting it all together.
        public static void RunUI()
        {
            // Our simple search GUI.
            var inputTextBox = new TextBox() { Width = 300 };
            var searchResultLB = new ListBox { Top = inputTextBox.Height + 10, Width = inputTextBox.Width };
            var searchStatus = new Label { Top = searchResultLB.Height + 30, Width = inputTextBox.Width };
            var mainForm = new Form { Controls = { inputTextBox, searchResultLB, searchStatus }, Width = inputTextBox.Width + 20 }; 

            // Our UI update handlers.
            var syncContext = SynchronizationContext.Current;
            Action<Action> onUITread = (x) => syncContext.Post(_ => x(), null);
            Action<string> onSearchStarted = t => onUITread(() => searchStatus.Text = (string.Format("searching for '{0}'.", t)));
            Action<string[]> onSearchResult = w => { 
                searchResultLB.Items.Clear(); 
                searchResultLB.Items.AddRange(w);
                searchStatus.Text += string.Format(" {0} maches found.", w.Length > 0 ? w.Length.ToString() : "No");
            };

            // Connecting input to search
            var input = ThrottleInput(TextChanged(inputTextBox), 1, TimeSpan.FromSeconds(0.5)).Do(onSearchStarted);
            var result = PerformSearch(input, new FakeWordSearchService());

            // Running it
            using (result.ObserveOn(syncContext).Subscribe(onSearchResult, ex => Console.WriteLine(ex)))
                Application.Run(mainForm);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM