Search code examples
c#awesomium

Awesomium offscreen webview never load a page


public class Program
{
    static void Main(string[] args)
    {
        var session = WebCore.CreateWebSession(new WebPreferences { WebSecurity = false });
        var browser = WebCore.CreateWebView(1920, 3000, session, WebViewType.Offscreen);
        WebCore.ShuttingDown += WebCoreOnShuttingDown;
        browser.ConsoleMessage += BrowserOnConsoleMessage;
        browser.LoadingFrameComplete += BrowserOnLoadingFrameComplete;
        browser.DocumentReady += BrowserOnDocumentReady;
        browser.Source = new Uri("http://www.google.ru/");
        var error = browser.GetLastError();
        Console.ReadKey();
    }

    private static void BrowserOnConsoleMessage(object sender, ConsoleMessageEventArgs consoleMessageEventArgs)
    {

    }

    private static void WebCoreOnShuttingDown(object sender, CoreShutdownEventArgs coreShutdownEventArgs)
    {

    }

    private static void BrowserOnDocumentReady(object sender, UrlEventArgs urlEventArgs)
    {

    }

    private static void BrowserOnLoadingFrameComplete(object sender, FrameEventArgs frameEventArgs)
    {

    }
}

It does not work. None of these events ever fired. error is None. I'm sure I miss something obvious. Does the WebView should be additionally initialized somehow? I searched in Awesomium Wiki but didn't find any additional information.


Solution

  • I made some research and found instruction here

    Wait Until the Page Has Finished Loading

    while (view->IsLoading())
    web_core->Update();

    in .Net the WebCore.Update is deprecated and have a description:

    In a non-UI environment (or even in a UI application), you can now create a dedicated Thread for Awesomium and from that thread, use WebCore.Run to start auto-updating.

    so i created this code example:

    static void Main(string[] args)
    {
        Task t = new Task(() =>
        {
            WebCore.Initialize(new WebConfig(), true);
            WebView browser = WebCore.CreateWebView(1024, 768, WebViewType.Offscreen);
            browser.DocumentReady += browser_DocumentReady;
            browser.Source = new Uri("https://www.google.ru/");
            WebCore.Run();
        });
        t.Start();
        Console.ReadLine();
    }
    static void browser_DocumentReady(object sender, UrlEventArgs e)
    {
        Console.WriteLine("DocumentReady");
    }
    

    You can find more info in WebCore.Run description.