microsoft / Chakra-Samples

Repository for Chakra JavaScript engine related samples.
MIT License
216 stars 84 forks source link

'Runtime is active on another thread' error with Chakra and WebView control combination #46

Closed globeport closed 8 years ago

globeport commented 8 years ago

I have a UWP app and a page with a WebView control.

The WebView points to a version of the Monaco Editor (https://github.com/Microsoft/monaco-editor) embedded in my app:

WebViewEditor.Navigate(new Uri("ms-appx-web:///Monaco/index.html"));

All works well.

However if I then try to use the Chakra engine, I immediately get 'wrong thread' exceptions.

The following will fail on the second line attempting to create a new context:

var runtime = JavaScriptRuntime.Create(); var context = runtime.CreateContext();

If I remove the WebView navigation above, the chakra engine functions fine.

I wouldn't have expected there to be any interaction? I would have expected a new chakra runtime to operate in isolation and have no threading issues when used in combination with a WebView control. Very odd. Any ideas?

Thanks, Stuart

globeport commented 8 years ago

Just to add, there's nothing special about navigating to the Monaco editor. If I navigate to www.google.com the same thing happens. I'm going to take the example UWP project posted here, and try the above just to exclude anything particular to my project setup.

globeport commented 8 years ago

Yep I can replicate this with the UWP example.

Steps:

  1. Add a WebView control to MainPage.xaml
  2. Change MainPage.cs to this:
public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            WebView.DOMContentLoaded += WebView_DOMContentLoaded;
            WebView.Navigate(new Uri("http://www.google.com"));
        }

        private void WebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
        {
            new ChakraHost.ChakraHost().init();
        }
    }
  1. Change debugger type in project settings to Application Process: 'Managed Only'
  2. Run and step through execution. Call to Native.JsCreateContext(runtime, out context) will fail

This looks like a significant problem unless I'm missing something? Thanks.

obastemur commented 8 years ago

Error is normal since WebView also creates the Runtime.

globeport commented 8 years ago

OK so two runtimes. But they don't operate in isolation?

obastemur commented 8 years ago

One runtime, multiple context ?

globeport commented 8 years ago

OK, to rephrase, how do I use the WebView and Chakra Engine together without raising ''Runtime is active on another thread' errors. :) If I'm working with the Chakra engine yes I can create multiple contexts. The problem is creating contexts in the first place if I have a WebView active which seems to be hogging the runtime.

obastemur commented 8 years ago

Didn't use WebView. If you are on the same thread with WebView (which is IIRC to be main thread??) You may get the active context by calling JsGetCurrentContext and using that Context, you may grab the Runtime objectJsGetRuntime.

Just a suggestion. Didn't try it.

globeport commented 8 years ago

This assumes a shared runtime though. I thought the whole point was to be able to create separate isolated runtimes. Surely if I explicitly create a new runtime this should not interact with the runtime created by the WebView. Anyway I'll investigate a bit more, thanks.

obastemur commented 8 years ago

Great. Please share your experiences back. It may help others :) Keeping this open for a while then.

globeport commented 8 years ago

Please keep this open. I haven't yet found a way to use the WebView control and Chakra engine together in a UWP app. This seems like a significant issue. Hopefully someone will be able to shed some light.

Thanks

obastemur commented 8 years ago

/cc @munyirik ?

globeport commented 8 years ago

OK I think I've found the problem. In the example above I'm creating a runtime on the UI thread (the same thread as the WebView).

If I do one of the following I can successfully create a new context on a new runtime.

Either

1) Run the WebView on a separate thread ( some limitations)

2) Create the Chakra runtime on a separate thread. e.g. Task.Run(()=>new ChakraHost.ChakraHost().init()) in the above example;

Phew. Happy to close :)

obastemur commented 8 years ago

Perfect!