Welcome Guest Search | Active Topics | Sign In | Register

return value from javascript function causing problems, EO.WebBrowser Options
Sven Dhaens
Posted: Thursday, August 13, 2015 7:27:12 AM
Rank: Member
Groups: Member

Joined: 5/20/2015
Posts: 11
I am using the following in C# project with EO.WebBrowser.

The problem I am having:

Somehow the (int)webView.GetDOMWindow().InvokeFunction("NewLayer");
causes everything after it to be called x3 times.
The new MapLayer(..) is created and added x3 times with each a different index recieved from the webview Javascript.
The Index is correct. the fact that it gets triggered 3 times somehow is not...

The problem does not occur when assigning a int value to layerindex. "int layerIndex= 2" for example.
therefor I think it might be something to do with the webview or something?

What Am I doing wrong?

public void LinkToPictureboxHandle(System.IntPtr handle)
{
webView.Create(handle);
}
public void setHTMLPath(string path)
{
//webView.Url = (path);
webView.LoadUrlAndWait(path);
//Event to handle function requests from JS
webView.JSExtInvoke += new JSExtInvokeHandler(WebView_JSExtInvoke);
}

private void CreateLayer(ServiceLayerName layerName, string image, int anchorY, int anchorX)
{
if (webView.StatusMessage != null && !webView.IsLoading) {
int layerIndex = (int)webView.GetDOMWindow().InvokeFunction("NewLayer");
MapLayer newLayer = new MapLayer(layerIndex, layerName, image);
newLayer.anchorY = anchorY;
newLayer.anchorX = anchorX;
normalLayers.Add(newLayer);
}
}

the function that is called in my .html that I asign to the webview I have:

function NewLayer() {
var markerLayer = new Array();
markerLayers.push(markerLayer);
return markerLayers.indexOf(markerLayer);
}
eo_support
Posted: Thursday, August 13, 2015 8:08:39 PM
Rank: Administration
Groups: Administration

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

Just by this we can't tell much. Please try to isolate the problem into a test page and post the test page. You can replace your MapLayer with something simplier such as aert or console.log. After that you can post the page here and we will try to run it to see if we can reproduce the problem. Usually as soon as we can reproduce the problem, we can find out the root cause.

Thanks!
Sven Dhaens
Posted: Friday, August 14, 2015 2:18:46 AM
Rank: Member
Groups: Member

Joined: 5/20/2015
Posts: 11
Ok I'll try isolate it..
....
Ok this is what I've found.

While I request an index from the .html file a event is triggered elsewhere in my code calling the CreateLayeragain.

I have tried locking(synchronize) the code so that it waits for the previous CreateLayerto be finished first. (and then doesn't go through all the code again > based on the LayerExists() )
But somehow My locking doesn't work.

private Object thisLock = new Object();

private void CreateLayer(ServiceLayerName layername, string image)
{
lock (thisLock) {
if (!LayerExists(layername))
{
if (webView.StatusMessage != null && !webView.IsLoading)
{
int layerIndex = getLayerIndex();

MapLayer newLayer = new MapLayer(layerIndex, layername, image);
normalLayers.Add(newLayer);

}
}
}
private int getLayerIndex()
{
return (int)webView.GetDOMWindow().InvokeFunction("NewLayer");
}

Is the InvokeFunction asynchronous or something like that causing these problems.?
Perhaps something that makes sure to wait for the return value? Similar to webView.LoadUrlAndWait Method?
eo_support
Posted: Friday, August 14, 2015 2:45:08 PM
Rank: Administration
Groups: Administration

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

You need a lock and a flag to avoid this kind of problem because of the root of the problem is not a threading issue, it's a re-entry issue. Try to change your code like this:

Code: C#
private Object thisLock = new Object();
private bool creatingLayer = false;

private void CreateLayer(ServiceLayerName layername, string image)
{
    lock (thisLock)
    {
        if (creatingLayer)
            return;

        creatingLayer = true;

        ....do your stuff here....

        creatingLayer = false;
    }
}


Hope this helps.

Thanks!
Sven Dhaens
Posted: Monday, August 17, 2015 2:48:14 AM
Rank: Member
Groups: Member

Joined: 5/20/2015
Posts: 11
d'oh! Thx for pointing this out.

Works like a charm.
eo_support
Posted: Monday, August 17, 2015 1:34:34 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Great. Glad to hear that works for you! Please feel free to let us know if there is anything else.

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.