gpt4 book ai didi

c# - 如何取消 C# 3.5 中的异步委托(delegate)?

转载 作者:太空狗 更新时间:2023-10-29 19:40:04 25 4
gpt4 key购买 nike

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

我想做的是:

  1. 用户在文本框中键入单个搜索字符串。
  2. 我等待 0.5 秒,然后我开始调用我的委托(delegate),指向一个搜索方法。
  3. 如果用户再次键入一个字符,我想取消搜索并使用键入的新字符串开始新的搜索。
  4. 不得阻塞 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;
}
}

最佳答案

切换到 BackGroudWorker ,支持所有你需要的(NoUI Blocking,Cancellation ect,Progress Reporting..)

关于c# - 如何取消 C# 3.5 中的异步委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4313718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com