[英]Async call to load a html file from local disk to web browser control still hangs UI
I have a list view that is populated with the location of some html files on the local disk. 我有一个列表视图,其中填充了一些本地磁盘上的html文件的位置。 When an the list is double clicked it takes the first selected item and loads that file location to the web browser control.
双击列表时,它将选择第一个选定的项目并将该文件位置加载到Web浏览器控件中。 I have fiddled around with async/await to try and get the UI free while the file loads to the web browser but it still hangs the UI.
我已经摆弄async / await尝试在将文件加载到Web浏览器时获得免费的UI,但是它仍然挂起了UI。
private async void ListView1_DoubleClick(object sender, EventArgs e)
{
await LoadPage(listView1.SelectedItems[0].Text);
}
private async Task LoadPage(string uri)
{
Uri myURI = new Uri(uri);
await Task.Factory.StartNew(()=>webBrowser1.Navigate(myURI),CancellationToken.None,TaskCreationOptions.LongRunning,TaskScheduler.Default);
}
Code works, the UI is still hanging though. 代码工作正常,但是UI仍然挂起。
Task.Factory.StartNew
with TaskScheduler.Default
runs your code in a thread-pool thread. 使用
TaskScheduler.Default
Task.Factory.StartNew
在线程池线程中运行代码。 This is not supposed to happen for code that manipulates UI elements, like the WebBrowser
control. 对于操纵UI元素的代码(如
WebBrowser
控件),不应发生这种情况。 You could add the line bellow before Application.Run
, to protect yourself from such mistakes. 您可以在
Application.Run
之前添加以下行,以防止出现此类错误。
Control.CheckForIllegalCrossThreadCalls = true;
Related link: Why are we allowed to modify the form title from a thread pool thread? 相关链接: 为什么我们允许从线程池线程修改表单标题?
I don't think you can do much about the WebBrowser
control slowing down your UI. 我认为您对
WebBrowser
控件减慢UI的工作不多。 Maybe hiding it initially, ans showing in again on DocumentCompleted
? 也许最初是将其隐藏,然后在
DocumentCompleted
上再次显示?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.