如何取消在C#3.5的异步委托? [英] How can I cancel an asychronous delegate in C# 3.5?
本文介绍了如何取消在C#3.5的异步委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已搜查谷歌上下,但我找不到接近有关该主题的任何适当的信息。
我想要做的是这样的:
- 用户类型单一的搜索字符串中的文本框。
- 我等待0.5秒,然后我开始我的BeginInvoke委托指向搜索方法。
- 如果用户键入再次一个char我想取消搜索,并开始与输入的新字符串新的搜索。
- 的UI线程不能被堵塞!
我如何能做到使用C#3.5?
更新
查看
私人无效OnTextChanged(...)
{
如果(SearchFormatEvent!= NULL)
{
ICollection的<对象>集合= SearchFormatEvent(MySearchString);
//请在返回集合的东西
}
}
SearchProvider
//这是调用的异步搜索采取由用户输入的搜索字符串委托
公共委托的ICollection<对象> SearchInputTextStrategy< T>(字符串参数); 公共类SearchProvider:ISearchProvider
{
私人ITextView _view;
私人SearchInputTextStrategy<对象> searchInputDelegate; 公共SearchProvider(ITextView视图)
{
_view =视图。
_view.SearchFormatEvent + =新ConstructSearchFormatDelegate(CostructSearchFormat);
} 私人字符串SearchFormat(字符串参数)
{
//计算字符串 返回的String.Empty; // ...
} 公众的ICollection<对象> CostructSearchFormat(字符串参数)
{
VAR searchfilter = SearchFormat(参数); IAsyncResult的pendingOperation = searchInputDelegate.BeginInvoke(searchfilter,NULL,NULL); //我怎样才能取消异步委托? ICollection的<对象>结果= searchInputDelegate.EndInvoke(pendingOperation); 返回结果;
}
}
解决方案
切换到BackGroudWorker,是支持你所需要的(NOUI封锁,取消等,进展的报告。)
I have searched google up and down but I can not find nearly any proper information about that topic.
What I wanna do is this:
- User types a single search-string in a textbox.
- I wait 0.5 s then I start to BeginInvoke my delegate pointing to a search method.
- If the user types again a char I want to cancel the Search and begin a new search with the new string typed.
- The UI-Thread must not be blocked!
How can I do that using C# 3.5 ?
UPDATE:
View:
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;
}
}
解决方案
Switch to BackGroudWorker , is supports all you need ( NoUI Blocking , Cancellation ect, Progress Reporting..)
这篇关于如何取消在C#3.5的异步委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文