Welcome Guest Search | Active Topics | Sign In | Register

EO.Webbrowser and Asynchronous Javascript Options
Chadwick Chennault
Posted: Monday, May 23, 2016 2:35:45 PM
Rank: Newbie
Groups: Member

Joined: 3/1/2016
Posts: 3
I am trying to run some asynchronous javascript (javascript that uses promises) in a thread runner. However, it seems that the threadrunner ignores the promises and exits immediately. How do I get the threadrunner to wait until the promises resolve before exiting?

Code: C#
public static object InvokeJs(EO.WebBrowser.WebView webView, string jsMethodToCall,
            string parentObject = "controller", object[] args = null)
        {
            try
            {
                // The InvokeFunction code is not throwing errors properly (a bug?).
                // The hack-around is to use EvalScript to throw a direct cast exception if
                // the called function does not exist.
                var window = (JSObject) webView.GetDOMWindow();
                var function = (JSFunction) webView.EvalScript($"{parentObject}.{jsMethodToCall}");
                return function.Invoke(window, true, args);
            }
            catch (Exception)
            {
                // ReSharper disable once PossibleIntendedRethrow
                throw new Exception($"The function \"{jsMethodToCall}\" is invalid.");
            }

        public static object RunJsFromUrlInThreadrunner(string urlOrPath, string methodname,
            string parentObject = "controller", object[] arguments = null, ResourceHandler handler = null,
             JSExtInvokeHandler callback = null)
        {
            string message = null;
            using (var trunner = new ThreadRunner("JavascriptRunner"))
            {
                using (var webView = trunner.CreateWebView())
                {
                    BrowserOptions options = new BrowserOptions
                    {
                        EnableWebSecurity = false,
                        LoadImages = false,
                        
                    };
                    webView.SetOptions(options);
                    var returnValue = trunner.Send(() =>
                    {
                        try
                        {
                            if (callback != null) webView.JSExtInvoke += callback;
                            webView.ConsoleMessage += (s, e) =>
                            {
                                Debug.Print(e.Message);
                                if(e.Severity != ConsoleMessageSeverity.Error) return;
                                message = $"Threadrunner Javascript error:\n{e.Message}";
                                trunner.Stop();
                            };
                            if (handler != null) webView.RegisterResourceHandler(handler);
                            webView.LoadUrlAndWait(urlOrPath);
                            return InvokeAsyncJS(webView, methodname, parentObject, arguments);
                        }
                        catch (Exception e)
                        {
                            message = e.Message;
                            return null;
                        }
                    });
                    if (!string.IsNullOrEmpty(message))
                    {
                        throw new Exception(message);
                    }
                    return returnValue;
                }
            }
        }
eo_support
Posted: Monday, May 23, 2016 3:54:35 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,082
Hi,

You just have to write your code so that you only destroy the ThreadRunner after your JavaScript has finished running. You can use JavaScript extension in your JavaScript code to call back into the C# side to notify the C# that you are done on the JavaScript side so now it is the time to destroy the ThreadRunner. On your C# side you would wait for that notification and then destroy the ThreadRunner.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.