简体   繁体   中英

Webbrowser InvokeScript function is blocking UI thread in Windows Phone 8

I am invoking a Javascript function using Webbrowser in Windows Phone 8.

browserControl.InvokeScript(funtionName, args);

This javascipt function is internally calling a service to fetch the data, during the javascript call i am showing ProgressIndicator.

But i am not able to see ProgressIndicator because javascript call is stopping UI thread.

This problem may be because of Javascript function and my UI thread is running in same thread. So i tried to move my Javascript function call to Background thread, but it failed saying "Invalid cross thread Access"

I am able to see the UI after my ScriptNotify is invoked. Before that i am not able to see my UI

You can see my code snippets below:

Can any one please suggest me how can i overcome this problem?

My progress indicator code:

public void ActivateProgressIndicator(this PhoneApplicationPage currentPage, string progressIndicatorText)
        {
            ProgressIndicator progressIndicator = new ProgressIndicator();
            progressIndicator.Text = progressIndicatorText;
            progressIndicator.IsVisible = true;
            progressIndicator.IsIndeterminate = true;
            SystemTray.SetProgressIndicator(currentPage, progressIndicator);
        }

My JS fucnction is calling a WCF rest service internally, this function call will take 30 seconds 1 minute. And i am JS code to Background thread like below:

ThreadPool.QueueUserWorkItem(o =>
            {
                WebBrowser browserControl = new WebBrowser();
                browserControl.IsScriptEnabled = true;
                browserControl.Navigate(new Uri("/WebAssets/Main.html", UriKind.RelativeOrAbsolute));

                browserControl.InvokeScript("RetrieveAccounts",
                                                         "http://some.service.net/",
                                                         "Plain",
                                                         "arg1",
                                                         "arg2",
                                                         "arg3");
            });

I am getting Invalid cross thread error while i am creating the webbrowser control.

This error is also generally shown when invoking java script method which does some stuff inside webpage like asynchronous call.

In my case it helped to call method this way:

I have taken the WB_LoadCompleted event to fire the invokescript method. You may modify it according to you needs.

    private void WB_LoadCompleted(object sender, NavigationEventArgs e)
    {
        // Make sure the HTML document has loaded before attempting to
        // invoke script of the document page. You could set loadCompleted
        // to true when the LoadCompleted event on the WebBrowser fires.
        ActivateProgressIndicator(this, "hello");
        Dispatcher.BeginInvoke(() =>
            {
                //Give a call to your Browser.InvokeScript here. e.g.
                WB.InvokeScript("execScript", gestureHold);
            });
    }

Hope this helps you. cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM