|
Rank: Advanced Member Groups: Member
Joined: 6/3/2016 Posts: 32
|
Version - latest version available on nuget as this post is written. OS: Win10, Win Server 2012 x64; .NET 4.5, Any CPU.
I need to run some javascript code when the page loaded. It's nothing complex - just reading some javascript values, serialize more complex objects (JSON.stringify) - or even to use the console.log. Sometimes I open a new tab via javascript. To do this, I added a handler for the DocumentComplete event. Before it worked fine, now it doesn't anymore.
The test was just to read an integer variable or to write something to console and before each call I checked if I am allowed to run the scripts and the property returned always true. How I tried:
1. I tried to use the EvalScript or QueueScriptCall. Blocked; 2. At one moment, I was afraid I was doing some kind of re-entrance locking and I decided to put a timer to schedule the doc-complete handler 2 seconds later. Same effect; 3. I ran the test in a Task object (.net Task) - with the same effect. I checked each time the ManagedThreadId of the event and the one of the thread running the script and they were different; 4. I restarted visual studio and even rebooted the machine - I was thinking there might be a leak somewhere and a resource was blocked. The same effect; 5. I ran it on a relatively clean machine - same.
One thing I noticed: the system window - which contains the browser is still usable (move/minimize/maximize/resize/etc). However, the browser area - while is no longer usable (everything I click does not respond), it's still displayed correctly (there is no whitish layout when an application hangs and no longer responds).
All those tests used to run before - at least a while ago (6-9 months) when I evaluated the library. Sometimes they crashed with an exception saying that the Javascript call is not allowed - which was fine. But now, it just hangs.
So, my question is: is it correct to run javascript code in WebView event's handlers? The one I am mostly interested is DocumentComplete when all data is retrieved. And if not, any reliable way I can do this?
|
|
Rank: Advanced Member Groups: Member
Joined: 6/3/2016 Posts: 32
|
Ping! No one? I am the only one with this issue? No workaround?
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi, Please check the threadings. There are two rules for EO.WebBrowser to function properly: 1. Each WebView is associated to the thread in which it is created. You generally can not call any member method of a WebView from another thread; 2. The thread that the WebView is associated to must continue pumping Windows message. It should never be on a wait block such as waiting for an async task to complete. The WebView's own blocking functions such as EvalScript satisfy this rule by pumping window messages internally; As such you will definitely run into hangs if you are not careful with async tasks when using WebView. The best practice to evaluate script is to use QueueScriptCall with a complete callback. That function should never block. The callback will be called in the WebView's thread. If this does not help you resolving the problem, please try to isolate the problem into a test project and send the test project to us. See here for more details: http://www.essentialobjects.com/forum/test_project.aspxWe will look into it as soon as we have that. Thanks!
|
|
Rank: Advanced Member Groups: Member
Joined: 6/3/2016 Posts: 32
|
So, if I understood correctly, if I want to run any script after the control finished to load the html I should run it in the same thread that triggered the LoadCompleted event? I tried that. What I am doing: + after the page finished to be loaded I am creating a new Task where I run my code. It is mostly post-processing stuff which I don't want to affect user's experience. + when I need to run some javascript I am using the WebViewItem associated with that control and I call the EvalScript via Dispatcher.Invoke. Checking the logs, I see it's running in the same thread as the LoadComplete.
Is this correct? Normally, based on what I read from your previous post, it should be correct.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
Yes. LoadCompleted event is called in the UI thread.
Generally EvalScript should not cause UI issue because the actual JavaScript execution is not done by your UI thread. EvalScript just sends the JavaScript to the browser engine and then runs an internal message loop to wait for the result. However because of its blocking nature, it can cause other issues. So it is best for you to use QueueScriptCall to run the JavaScript asynchronously.
Thanks!
|
|
Rank: Advanced Member Groups: Member
Joined: 6/3/2016 Posts: 32
|
I tried all kind of approaches. From the event handler to open a new task. I even tried to run the Script object in a separate thread and it's the same. I managed to build a small "sample" if you want to take a look. Also, it would be nice a timeout value and an exception should be thrown (possible to contain a reference to the script that is blocking the flow) ...
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi, Please send the test app to us and we will be happy to take a look. See here for more details: http://essentialobjects.com/forum/test_project.aspxThanks!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
This is just to let you know that we have found the root cause of this issue. We will post a new build later this week with the fix. We will reply here again as soon as the new build is posted.
Thanks!
|
|
Rank: Advanced Member Groups: Member
Joined: 6/3/2016 Posts: 32
|
Looking forward to! Thanks!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
This is just to let you know that we have posted a new build that should fix this issue. You can download the new build from our download page. Please take a look and let us know how it goes.
Thanks!
|
|
Rank: Member Groups: Member
Joined: 3/17/2016 Posts: 12
|
i have a simple form with webView and have javascript code that's below some time a flash screen comes for only mili second and immediately close i record while running this my application and take a screen shot from this video WebView.EvalScript("document.getElementsByTagName('a').length")) WebView.EvalScript("document.getElementsByTagName('input')[11"].focus()") WebView.EvalScript("document.getElementsByTagName('a')[12].click()") every time this flash comes on different javascript. this flash screen not show all time but mostly it comes here i am attaching this flash screen https://postimg.org/image/ij6f8wgnj/
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
joinyasin wrote:i have a simple form with webView and have javascript code that's below some time a flash screen comes for only mili second and immediately close i record while running this my application and take a screen shot from this video WebView.EvalScript("document.getElementsByTagName('a').length")) WebView.EvalScript("document.getElementsByTagName('input')[11"].focus()") WebView.EvalScript("document.getElementsByTagName('a')[12].click()") every time this flash comes on different javascript. this flash screen not show all time but mostly it comes here i am attaching this flash screen https://postimg.org/image/ij6f8wgnj/ Hi, Please try to isolate the problem into a test project and send the test project to us. See here for more details: http://www.essentialobjects.com/forum/test_project.aspxAlso in the future if you have a new question, please start a new thread. Do not reply in an unrelated thread with a new question. Thanks!
|
|
Rank: Advanced Member Groups: Member
Joined: 6/3/2016 Posts: 32
|
My issue was solved. It works fine now. Thank you.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Great. Thanks for confirming the fix!
|
|