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

如何取消C#3.5中的异步委托?,c#,asynchronous,delegates,C#,Asynchronous,Delegates,我在谷歌上下搜索过,但几乎找不到任何关于这个主题的正确信息 我想做的是: 用户在文本框中键入单个搜索字符串 我等待0.5秒,然后开始唤醒指向搜索方法的代理 如果用户再次键入字符,我希望取消搜索,并使用键入的新字符串开始新的搜索 不能阻止UI线程 我如何使用C#3.5做到这一点 更新: private void OnTextChanged(...) { if (SearchFormatEvent != null) { ICollection<object> c

我在谷歌上下搜索过,但几乎找不到任何关于这个主题的正确信息

我想做的是:

  • 用户在文本框中键入单个搜索字符串
  • 我等待0.5秒,然后开始唤醒指向搜索方法的代理
  • 如果用户再次键入字符,我希望取消搜索,并使用键入的新字符串开始新的搜索
  • 不能阻止UI线程 我如何使用C#3.5做到这一点

    更新

    private void OnTextChanged(...)
    {
       if (SearchFormatEvent != null)
       {
           ICollection<object> collection = SearchFormatEvent("MySearchString");
           // Do stuff on the returned collection                            
        }
    }
    
    // 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;                
            }
        }
    
    查看

    private void OnTextChanged(...)
    {
       if (SearchFormatEvent != null)
       {
           ICollection<object> collection = SearchFormatEvent("MySearchString");
           // Do stuff on the returned collection                            
        }
    }
    
    // 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;                
            }
        }
    
    private void OnTextChanged(…)
    {
    if(SearchFormatEvent!=null)
    {
    ICollection collection=SearchFormatEvent(“MySearchString”);
    //对返回的集合进行处理
    }
    }
    
    搜索提供者

    private void OnTextChanged(...)
    {
       if (SearchFormatEvent != null)
       {
           ICollection<object> collection = SearchFormatEvent("MySearchString");
           // Do stuff on the returned collection                            
        }
    }
    
    // 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;                
            }
        }
    
    //这是为异步搜索调用的委托,使用用户键入的searchstring
    公共委托ICollection searchInputExtStrategy(字符串参数);
    公共类SearchProvider:ISearchProvider
    {
    私有ITextView\u视图;
    私有SearchInputExtrategy searchInputDelegate;
    公共搜索提供程序(ITextView视图)
    {
    _视图=视图;
    _view.SearchFormatEvent+=新的ConstructSearchFormatDelegate(CostructSearchFormat);
    } 
    私有字符串搜索格式(字符串参数)
    { 
    //计算字符串
    返回字符串。空;//。。。
    }
    公共ICollection CostructSearchFormat(字符串参数)
    {
    var searchfilter=SearchFormat(参数);
    IAsyncResult pendingOperation=searchInputDelegate.BeginInvoke(“searchfilter”,null,null);
    //如何取消异步委托?
    ICollection result=searchInputDelegate.EndInvoke(pendingOperation);
    返回结果;
    }
    }
    
    切换到BackgroundWorker,is支持您所需的一切(NoUI阻止、取消等、进度报告)

    看看,这是一种线程安全的取消信号方法


    您使用
    CancellationTokenSource
    CancellationToken
    (您的搜索线程)的所有所有者发出取消信号
    CancellationToken

    您是如何生成后台搜索方法的?我已经更新了一个代码示例!看上面!不能以干净的方式停止非合作函数的执行。您需要此函数定期检查的标志
    CancellationToken
    在.net 4中执行此操作,不知道3.5中是否有内置类。@Pascal-能否共享完整解决方案的源代码?