2010-11-30 2 views
8

Google을 위아래로 검색했지만 해당 주제에 대한 적절한 정보를 거의 찾을 수 없습니다. 텍스트 상자에C# 3.5에서 비동기 대리자를 취소하려면 어떻게해야합니까?

  1. 사용자 유형 단일 검색 문자열 :

    는 내가 뭘하고 싶어하는 것은 이것이다.

  2. 0.5 초 기다렸다가 검색 메서드를 가리키는 대리자를 BeginInvoke 시작합니다.
  3. 사용자가 char을 다시 입력하면 검색을 취소하고 입력 한 새 문자열로 새 검색을 시작합니다.
  4. UI 스레드를 차단해서는 안됩니다!

C# 3.5를 사용하여 어떻게 할 수 있습니까?

UPDATE :

보기 :

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;     
     } 
    } 
+1

어떻게 당신이 배경 검색 방법을 산란하는에 취소를 알리기 위해 CancellationTokenSource를 사용할 수 있습니까? – Lazarus

+0

코드 샘플을 업데이트했습니다! 위 참조! – Pascal

+0

비협조적인 기능의 실행을 깨끗하게 중단 할 수는 없습니다. 이 기능이 정기적으로 검사하는 플래그가 필요합니다. 'CancellationToken'은 .net 4에서 이것을 수행합니다. 3.5에 내장 클래스가 있는지 전혀 모릅니다. – CodesInChaos

답변

5

BackGroudWorker로 전환이며, 당신이 필요로하는 모든 지원 (NoUI 차단, 취소 요법, 진행보고 ..)

+0

나는 그것을 사용할 것이라고 생각한다. 나는 완전히 잘못된 트랙에있었습니다 ... – Pascal

+0

@Pascal - 완전한 솔루션을위한 소스 코드를 공유 할 수 있습니까? –

1

CancellationTokenSourceCancellationToken을 살펴보면, 취소 신호를 보내는 스레드로부터 안전한 방법입니다.

당신은 (귀하의 경우 검색 실) CancellationToken의 모든 소유자

+1

귀하의 2 링크는 .NET 4.0을 의미합니까? 여기 3.5 만 사용합니다! – Pascal

+0

오케이, 개봉 된, 죄송합니다. – thumbmunkeys

관련 문제