-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OffScreen - Clarify BrowserInitialized may not be called if Browser created automatically #3552
Comments
Please edit your original question and fill out the bug report temple available at https://github.com/cefsharp/CefSharp/blob/master/.github/ISSUE_TEMPLATE/bug_report.md#bug-report |
What version of the product are you using? What architecture x86 or x64? What version of .Net? On what operating system? Are you using WinForms, WPF or OffScreen? What steps will reproduce the problem? What is the expected output? What do you see instead? Please provide any additional information below. |
As per the bug report, did you check the log file? Have you tested on different hardware? |
As per the bug report, did you check the log file?
Have you tested on different hardware? |
The behaviour suggests a problem with CEF/Chromium. I'd suggest trying the following command line args. settings.CefCommandLineArgs.Add("disable-gpu");
settings.CefCommandLineArgs.Add("disable-gpu-compositing"); Are you upgrade from a previous version that was working? |
The behaviour suggests a problem with CEF/Chromium. I'd suggest trying the following command line args. Are you upgrade from a previous version that was working? The reason I want to do this is to create a fresh new incognito window using this code chrome.Dispose();
var context = new RequestContext();
chrome = new CefSharp.OffScreen.ChromiumWebBrowser("",new BrowserSettings(),context); is there a workaround that has the same effect as destroying the window and creating a new one? |
Create/Dispose of 1000 OffScreen ChromiumWebBrowser instances Issue #3552
On average how many iterations of the loop are you seeing? I've run the following five times now without issues and it creates 2000 browser instances. // Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using CefSharp.OffScreen;
using CefSharp.Web;
using System;
using System.Threading.Tasks;
namespace CefSharp.MinimalExample.OffScreen
{
public static class Program
{
public static async Task<int> Main(string[] args)
{
for (int i = 0; i < 2000; i++)
{
Console.WriteLine("Create ChromiumWebBrowser #" + i);
using (var browser = new ChromiumWebBrowser(new HtmlString("Testing")))
{
await browser.LoadPageAsync();
var source = await browser.GetSourceAsync();
if(source.Contains("Testing"))
{
Console.WriteLine("Source Loaded Successfully");
}
else
{
throw new Exception("Html Source doesn't match expected output.");
}
}
}
Console.WriteLine("Press enter to continue.");
Console.ReadLine();
return 0;
}
public static Task LoadPageAsync(this IWebBrowser browser)
{
//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
//and switch to tcs.TrySetResult below - no need for the custom extension method
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
EventHandler<LoadingStateChangedEventArgs> handler = null;
handler = (sender, args) =>
{
//Wait for while page to finish loading not just the first frame
if (!args.IsLoading)
{
browser.LoadingStateChanged -= handler;
//This is required when using a standard TaskCompletionSource
//Extension method found in the CefSharp.Internals namespace
tcs.TrySetResult(true);
}
};
browser.LoadingStateChanged += handler;
return tcs.Task;
}
}
}
What exactly are you trying to achieve by creating a new |
On average how many iterations of the loop are you seeing? I've run the following five times now without issues and it creates 2000 browser instances. using System;
using System.Threading;
namespace Test
{
class Program
{
static void Main(string[] args)
{
CefSharp.Cef.Initialize(new CefSharp.OffScreen.CefSettings());
int ok = 0;
while (true)
{
bool init = false;
var chrome = new CefSharp.OffScreen.ChromiumWebBrowser();
chrome.BrowserInitialized += (s, e) =>
{
init = true;
};
while (!init)
{
Thread.Sleep(5);
}
chrome.Dispose();
ok++;
Console.WriteLine("Init OK: " + ok);
}
}
}
} What exactly are you trying to achieve by creating a new RequestContext? Clean cache? Deleting cookies? Local storage? Need more detail. |
Did you run the code example I provided? There's a potential for your code to be problematic, theoretically the underlying int ok = 0;
while (true)
{
bool init = false;
var chrome = new ChromiumWebBrowser(automaticallyCreateBrowser:false);
chrome.BrowserInitialized += (s, e) =>
{
init = true;
};
chrome.CreateBrowser();
while (!init)
{
Thread.Sleep(5);
}
chrome.Dispose();
ok++;
Console.WriteLine("Init OK: " + ok);
} I only have an old |
Create/Dispose of 1000 OffScreen ChromiumWebBrowser instances Issue #3552
I've updated the title as I believe the problem is related to timing and the xml doc should be updated to clarify this behaviour. The browser created automatically via the constructor may result in the browser created before the |
I have tried your code and for now it looks like it solved the issue. I'll update you if it stops working again or if i encounter an issue or find more information about it. You can close the issue for the time being but if not that's fine. Thank you for the help tho. |
…rowserCreated delegate Use the constructor overload as an alternative to the BrowserInitialized event Follow up to #3552
Xml doc has been updated in commit 2568236 Open to suggestions on wording improvements. I've also added a constructor overload in commit 4ebe7de that takes an using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext, onAfterBrowserCreated: (b) =>
{
//Browser has been created (initialized)
var ident = b.Identifier;
}))
{
await LoadPageAsync(browser);
} |
…reated automatically Resolves #3552
…rowserCreated delegate Use the constructor overload as an alternative to the BrowserInitialized event Follow up to #3552
Hello,
I don't know how to exactly reproduce this but the code I came up with creates a new window and after finishing initialization disposes it. The problem is after some time the chromium browser doesn't get initialized anymore which happens after a random amount of successful initializations.
The text was updated successfully, but these errors were encountered: