私はgoogleを検索しましたが、そのトピックに関するほぼすべての適切な情報を見つけることができません。テキストボックスにC#3.5で非同期デリゲートをキャンセルするにはどうすればよいですか?
- ユーザータイプ単一の検索文字列:
は、私は何をしたいことはこれです。
- 私は0.5秒待ってから、BeginInvokeを呼び出して、代理人が検索方法を指し示すようにします。
- ユーザーがもう一度char型を入力した場合、検索をキャンセルして、入力した新しい文字列で新しい検索を開始します。
- 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;
}
}
どのようにして、バックグラウンド検索方法を生み出していますか? – Lazarus
コードサンプルを更新しました!上記を参照! – Pascal
協力していない関数の実行をクリーンな方法で停止することはできません。この機能が定期的にチェックするフラグが必要です。 'CancellationToken'は.net 4でこれを行いますが、3.5に組み込みクラスがあるかどうかは分かりません。 – CodesInChaos