當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'循環,你需要?我錯過了什麼嗎? –
http://msdn.microsoft.com/it-it/library/system.asynccallback.aspx – Saturnix
@SriramSakthivel你不會錯過任何東西,這是因爲我不知道傳遞一個委託作爲參數並在foreach循環是8-)你能把這個帖子作爲答案嗎? – jskidd3