How to load a child view in a headless browser using Awesomium?

I developed a C# console application utilizing Awesomium 1.7.4 to create a WebView that captures screenshots of some dynamic web pages. While everything was functioning properly, I encountered a challenge after implementing an event handler for ShowCreatedWebView, which is activated when a new view is initiated. In my scenario, this event occurs during a form submission which leads to a file download page. Following the addition of this handler, an error surfaced during the initial WebView loading: ‘The WebCore is not initialized.’ Here’s the crucial part of my code:

Handlers:

WebCore.Download += onFileDownload;
webView.ShowCreatedWebView += onNewViewRequest;
webView.FrameLoadingCompleted += (s, e) => {
    loadingError = false;
    isLoadComplete = true;
};
webView.FrameLoading += (s, e) => {
    loadingError = false;
    isLoadComplete = false;
};
webView.FrameLoadingFailed += handleLoadingFailure;

The handler for ShowCreatedWebView:

private static void onNewViewRequest(object sender, ShowCreatedWebViewEventArgs e) {
    var downloadWebView = new WebView(e.NewViewInstance);
    downloadWebView.Resize(1024, 768);
    surface = (BitmapSurface)downloadWebView.Surface;
    surface.SaveImage(opt_target + "downloadView.png", true);
}

At the point of triggering ShowCreatedWebView, the primary WebView code is as follows:

isLoadComplete = false;
string jsTriggerDownload = HelperParser.jsLibActions("start-download-files");
webView.ExecuteJavascriptWithResult(jsTriggerDownload); //TRIGGERS ShowCreatedWebView
while (!isLoadComplete) {
    Thread.Sleep(Waits["loading-time"]);
    WebCore.Update(); //ERROR HAPPENS HERE AFTER ShowCreatedWebView TRIGGERS
}

What am I overlooking?

The error ‘The WebCore is not initialized’ usually means that the WebCore has not been started properly before trying to use it. There are several things you can check or improve in your existing implementation to ensure that WebCore is initialized correctly before WebView operations are performed. Here are some steps to troubleshoot and potentially resolve the issue:

  1. Ensure WebCore Initialization: Confirm that WebCore is properly initialized at the start of your application. This is typically done as follows:
if (!WebCore.IsRunning)
{
    WebCore.Initialize(new WebConfig { LogPath = "log", LogLevel = LogLevel.Verbose });
}
  1. Start WebCore before creating WebViews: Make sure WebCore is started before trying to create or interact with WebView instances.
if (!WebCore.IsRunning)
{
    WebCore.Initialize();
}
var webView = WebCore.CreateWebView(1024, 768);
  1. Check for WebCore updates: WebCore.Update() should be called regularly in the main loop of your application to process tasks like creating new views.

  2. Handling Showing New View: Ensure ShowCreatedWebView creates reusable instances for child views and your logic is sound.

private static void onNewViewRequest(object sender, ShowCreatedWebViewEventArgs e) {
    var childWebView = WebCore.CreateWebView(1024, 768);
    e.NewViewInstance = childWebView.InstanceId;

    // Optionally handle download tasks
}
  1. Synchronize WebView operations: Make sure operations on the WebView are thread-safe and executed in the same context as WebCore updates.

  2. Gracefully handling completions: Reset isLoadComplete correctly to reflect WebView’s actual loading state properly.

By following these steps, you can ensure that the WebCore initialization is seamlessly handled, preventing ‘not initialized’ errors from occurring when loading child views or handling downloads.