我创建了一个小型 C# 应用程序,它应该异步地从 XML 文件中提取所有值。问题是..它不是异步的,我看不出哪里出错了。单击按钮时,UI 冻结,应用程序无法移动等,显示出它同步运行的所有迹象。
谁能明白为什么会这样?
private async void parseAndExportBtn_Click(object sender, EventArgs e)
{
progressBar1.MarqueeAnimationSpeed = 100;
parseAndExportBtn.Enabled = false;
selectDirectoryBtn.Enabled = false;
status.Text = "Started searching files...";
await SearchFiles(selectTxcDirectory.SelectedPath);
status.Text = "Finished searching files";
}
private static async Task SearchFiles(string path)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path + "/cen_18-2_-1-y11.xml");
using (XmlReader r = XmlReader.Create(new StringReader(xmlDoc.InnerXml), new XmlReaderSettings() { Async = true }))
{
while (await r.ReadAsync())
{
switch (r.NodeType)
{
case XmlNodeType.Text:
Console.WriteLine(await r.GetValueAsync());
break;
}
}
}
}