Search code examples
c#multithreadingvisual-studiowebviewawesomium

Awesomium Multithreading error


I made this little code:

private void button1_Click(object sender, EventArgs e)
{
    Task t = new Task(() =>
    {
        tt += "a";
        WebCore.Initialize(new WebConfig(), true);
        WebView browser = WebCore.CreateWebView(1024, 768, WebViewType.Offscreen);
        browser.DocumentReady += browser_DocumentReady;
        browser.Source = new Uri("https://www.google.com/");
        WebCore.Run();
    });
    t.Start();
    Console.ReadLine();
}

static void browser_DocumentReady(object sender, UrlEventArgs e)
{
    Console.WriteLine("DocumentReady");
}

To try to do some multi-threading with different web views doing the same stuff at the same time.

Every time I press the button once it works fine and shows me "DocumentReady" in the console but when I press it one more time, it gives me this error:

System.InvalidOperationException: 'The WebCore is already initialized.'

It makes a lot of sense because I have already initialized it once before, but is there a way to run this script multiple times?


Solution

  • awesomium needs to run only in one thread. there is no workaround for this. you cant start multiple instances from different threads. though all navigation will occur in different seperate processes but events will be plumbed up within main thread (fyi if you use winforms it must be gui thread)

    if you want sessions (cookies) with webclient you can use seperate CookieContainer with each session