我上下搜索了谷歌,但几乎找不到有关该主题的任何正确信息。
我想做的是这样的:
- 用户在文本框中键入单个搜索字符串。
- 我等待 0.5 秒,然后开始 BeginInvoke 指向搜索方法的委托。
- 如果用户再次键入一个字符,我想取消搜索并使用键入的新字符串开始新搜索。
- UI线程一定不能被阻塞!
我怎样才能使用 C# 3.5 做到这一点?
UPDATE:
View:
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;
}
}
切换到 BackGroundWorker ,支持您所需的一切(NoUI 阻止、取消等、进度报告..)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)