【发布时间】:2017-11-30 06:18:28
【问题描述】:
当foreach 循环完成对List<> 中的每个项目的搜索时,我需要触发回调。
private async void startSearchBtn_Click(object sender, EventArgs e)
{
await Search(files, selectTxcDirectory.SelectedPath, status);
}
private static async Task Search(List<string> files, string path, Label statusText)
{
foreach (string file in files)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
statusText.Text = "Started scanning...";
using (XmlReader reader = XmlReader.Create(new StringReader(xmlDoc.InnerXml), new XmlReaderSettings() { Async = true }))
{
while (await reader.ReadAsync())
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "LineName"))
{
Console.WriteLine(reader.ReadInnerXml());
}
}
}
}
}
这可能吗?如果可以,怎么做?
【问题讨论】:
-
为什么不将委托作为参数传递并在您需要的
foreach循环中调用它?我错过了什么吗? -
@SriramSakthivel 你没有遗漏任何东西,这是因为我不知道将代表作为参数传递并在 foreach 循环中调用它是什么 8-) 你能把这个作为答案发布吗?
标签: c# async-await