如何在C#3.5中取消异步委托? [英] How can I cancel an asynchronous delegate in C# 3.5?

查看:143
本文介绍了如何在C#3.5中取消异步委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google上下搜索了,但是我几乎找不到有关该主题的任何适当信息。

I have searched google up and down but I can not find nearly any proper information about that topic.

我想做的是:


  1. 用户在文本框中输入单个搜索字符串。

  2. 我等待0.5秒,然后开始BeginInvoke我的代表

  3. 如果用户再次键入一个字符,我想取消搜索并使用键入的新字符串开始新的搜索。

  4. 不得阻止UI线程!

  1. User types a single search-string in a textbox.
  2. I wait 0.5 s then I start to BeginInvoke my delegate pointing to a search method.
  3. If the user types again a char I want to cancel the Search and begin a new search with the new string typed.
  4. The UI-Thread must not be blocked!

如何使用C#3.5做到这一点?

How can I do that using C# 3.5 ?

更新

查看

private void OnTextChanged(...)
{
   if (SearchFormatEvent != null)
   {
       ICollection<object> collection = SearchFormatEvent("MySearchString");
       // Do stuff on the returned collection                            
    }
}

SearchProvider

// This is the delegate invoked for the async search taking the searchstring typed by the user
    public delegate ICollection<object> SearchInputTextStrategy<T>(string param);

    public class SearchProvider : ISearchProvider
    {
        private ITextView _view;
        private SearchInputTextStrategy<object> searchInputDelegate;

        public SearchProvider(ITextView view)
        {
            _view = view;
            _view.SearchFormatEvent += new ConstructSearchFormatDelegate(CostructSearchFormat);
        } 

        private string SearchFormat(string param)
        { 
            // compute string

            return string.Empty; //...
        }

        public ICollection<object> CostructSearchFormat(string param)
        {
            var searchfilter = SearchFormat(param);

             IAsyncResult pendingOperation = searchInputDelegate.BeginInvoke("searchfilter",null,null);

            // How can I cancel the Async delegate ?

            ICollection<object> result = searchInputDelegate.EndInvoke(pendingOperation);

            return result;                
        }
    }


推荐答案

切换到BackGroudWorker,它支持您所需要的一切(NoUI阻止,取消等,进度报告。。)

Switch to BackGroudWorker , is supports all you need ( NoUI Blocking , Cancellation ect, Progress Reporting..)

这篇关于如何在C#3.5中取消异步委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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