如何在C#3.5中取消异步委托?

Pas*_*cal 8 c# delegates asynchronous

我上下搜索谷歌但我找不到关于该主题的任何适当信息.

我想做的是:

  1. 用户在文本框中键入单个搜索字符串.
  2. 我等了0.5秒然后我开始BeginInvoke指向搜索方法的委托.
  3. 如果用户再次键入char,我想取消搜索并开始新的搜索,并输入新的字符串.
  4. 不得阻止UI-Thread!

我怎么能用C#3.5做到这一点?

更新:

查看:

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

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;                
        }
    }
Run Code Online (Sandbox Code Playgroud)

Tal*_*ner 5

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