我有一段代码可以搜索多个第三方API.我根据搜索条件将搜索分为两组.我开始两次搜索都是因为每次搜索都非常及时,但如果第一组搜索结果匹配,我不想等待第二个搜索组完成.基本上我所拥有的是:
Dictionary<string, string> result = null;
NameSearchDelegate nameDel = new NameSearchDelegate(SearchByName);
IAsyncResult nameTag = nameDel.BeginInvoke(name, null, null);
if(!string.IsNullOrWhiteSpace(telNum))
{
result = SearchByTelNum(telNum);//Will return null if a match is not found
}
if(null == result)
{
result = nameDel.EndInvoke(nameTag);
}
//End the delegate to prevent memory leak
//else
//{
// nameDel.EndInvoke(nameTag)
//}
return result;
Run Code Online (Sandbox Code Playgroud)
所以我想在调用SearchByTelNum之前启动SearchByName,以防它找不到匹配项,但是如果找到匹配项我不想在返回匹配项之前等待SearchByName完成.如果我不再需要它的结果,有没有办法简单地结束或取消该委托?