Skip to content
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

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

Closed
globeport opened this issue Nov 2, 2016 · 13 comments

Comments

@globeport
Copy link

globeport commented Nov 2, 2016

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 globeport changed the title 'Runtime is active on another thread' error with Chackra and WebView combination 'Runtime is active on another thread' error with Chakra and WebView control combination Nov 2, 2016
@globeport
Copy link
Author

globeport commented Nov 2, 2016

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
Copy link
Author

globeport commented Nov 2, 2016

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
Copy link
Contributor

Error is normal since WebView also creates the Runtime.

@globeport
Copy link
Author

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

@obastemur
Copy link
Contributor

One runtime, multiple context ?

@globeport
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

globeport commented Nov 2, 2016

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
Copy link
Contributor

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

@globeport
Copy link
Author

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
Copy link
Contributor

/cc @munyirik ?

@globeport
Copy link
Author

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
Copy link
Contributor

Perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants