简体   繁体   English

从本地磁盘将html文件加载到Web浏览器控件的异步调用仍然挂起UI

[英]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.

相关问题 从 microsoft edge 而不是 internet explorer 中的 web 浏览器控件打开本地 html 文件 - open a local html file from web browser control in microsoft edge instead of internet explorer 在 Xamarin Forms 的本机 Web 浏览器中加载本地 html 文件 - Load local html file in native web browser in Xamarin Forms UWP C#:从本地磁盘打开HTML文件到默认浏览器 - UWP C#: Open HTML file from local disk to default browser 在与 Web 浏览器控件(visual studio)中的可执行文件相同的目录中运行本地 .html 文件 - Run local .html file in same directory as executable in web browser control (visual studio) 使用WebBrowser控件从磁盘加载多个HTML文件 - Using the WebBrowser control to load several HTML files from disk Web浏览器控件使我的应用程序挂起 - Web browser control hangs my application 从Web服务下载文件后,使用Web浏览器控件在html中播放Java脚本时的奇怪行为 - Odd behavior on playing java script in a html using web browser control after download a file from the web service 从浏览器调用Web API不适用于异步操作 - call to web API from browser not working with Async action CefSharp WinForms-加载本地html文件时浏览器控件冻结 - CefSharp WinForms - browser control freezing when loading local html file 异步方法挂起UI - Async method hangs UI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM