如何解决多次打开多个网页后,经常会冻结
我正在开发一个Web搜寻器,它将经常请求新的网页。在测试过程中,程序经常冻结。
我写了一个测试用例,解释了我的用法和出现问题的情况。
有时,当它打开一个网页10次时,可能会在它打开20次时卡住。我不确定打开网页有多少次。
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
ChromiumWebBrowser browser;
System.Timers.Timer timer;
int i = 1;
public Form1()
{
InitializeComponent();
timer = new System.Timers.Timer(400) { AutoReset = true };//Timer,automatically open a new web page
timer.Elapsed += Timer_Elapsed;
browser = new ChromiumWebBrowser("www.baidu.com") { Parent = splitContainer1.Panel1,Dock = DockStyle.Fill };
browser.LifeSpanHandler = new LifeSpanHandler(splitContainer1.Panel2);
browser.FrameLoadEnd += Browser_FrameLoadEnd;
}
private void Browser_FrameLoadEnd(object sender,FrameLoadEndEventArgs e)
{
if (e.Frame.IsMain == false) return;
timer.Start();
}
private void Timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
{
i = i > 1 ? 1 : i + 1;
browser.ExecuteScriptAsync("document.getElementsByClassName('mnav c-font-normal c-color-t')[" + (i) + "].click()");//Open a link in the web page
}
}
class LifeSpanHandler : ILifeSpanHandler
{
Control host;
internal LifeSpanHandler(Control host)
{
this.host = host;
}
public bool DoClose(IWebBrowser chromiumWebBrowser,IBrowser browser)
{
return false;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser,IBrowser browser)
{
}
public void OnBeforeClose(IWebBrowser chromiumWebBrowser,IBrowser browser)
{
}
public bool OnBeforePopup(IWebBrowser chromiumWebBrowser,IBrowser browser,IFrame frame,string targetUrl,string targetFrameName,WindowOpenDisposition targetDisposition,bool userGesture,IPopupFeatures popupFeatures,IWindowInfo windowInfo,IBrowserSettings browserSettings,ref bool noJavascriptAccess,out IWebBrowser newBrowser)
{
newBrowser = null;
var webBrowser = (ChromiumWebBrowser)chromiumWebBrowser;
webBrowser.Invoke(new Action(() =>
{
if (host.HasChildren) host.Controls[0].Dispose();
var control = new Control { Parent = host,Dock = DockStyle.Fill };
control.CreateControl();
windowInfo.SetAsChild(control.Handle);
}));
return false;
}
}
}
测试代码放置在GitHub上,这是Visual Studio 2019项目。我们希望您可以在本地执行项目,以便更直观地发现问题。非常感谢。
https://github.com/haohaodz/cefsharp-form-vs2019-problem.git
解决方法
您是否已与其他网站进行了测试?
对其他网站的频繁请求也会导致死锁。
问题是否通过github.com/cefsharp/CefSharp.MinimalExample重现
该问题不会在github.com/cefsharp/CefSharp.MinimalExample中重现。
在BrowserTabUserControl.cs
中browser.LifeSpanHandler = new LifeSpanHandler(openPopupsAsTabs: false);
默认情况下会在新窗口中打开网页。频繁打开多个网页,不会重现冻结问题。
我将openPopupsAsTabs更改为true,然后在标签页中打开新页面
browser.LifeSpanHandler = new LifeSpanHandler(openPopupsAsTabs: true);
频繁打开多个网页,程序将冻结。 程序冻结在dispose方法中:
System.Windows.Forms .dll! System.Windows.Forms . UnsafeNativeMethods.DestroyWindow (
System.Runtime.InteropServices . handleref hWnd) unknown
System.Windows.Forms .dll! System.Windows.Forms . NativeWindow.DestroyHandle () unknown
System.Windows.Forms .dll! System.Windows.Forms . Control.DestroyHandle () unknown
System.Windows.Forms .dll! System.Windows.Forms . Control.Dispose (bool disposing) unknown
System.dll ! System.ComponentModel.Component . dispose() unknown
有时候程序突然退出,有一个例外:
Exception thrown:“ System.IO.PipeException ”(located at System.ServiceModel.dll (2)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.dll (2)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.dll (2)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.dll (2)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.Internals . DLL)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.Internals . DLL)
Exception thrown:“ System.ServiceModel.CommunicationObjectFaultedException ”(located at System.ServiceModel.dll (2)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。