谁能明白为什么会这样?
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; } } }}解决方法 在任务中调用Searchfiles(字符串路径).如下.
private voID parseAndExportBtn_Click(object sender,EventArgs e){ progressbar1.MarqueeAnimationSpeed = 100; parseAndExportBtn.Enabled = false; selectDirectoryBtn.Enabled = false; status.Text = "Started searching files..."; Task t = Task.Run(() => Searchfiles(selectTxcDirectory.Selectedpath)); status.Text = "Finished searching files";}
我建议您在异步上等待read this article并等待关键字.
总结以上是内存溢出为你收集整理的C#app应该是异步的,但UI仍然会冻结全部内容,希望文章能够帮你解决C#app应该是异步的,但UI仍然会冻结所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)