|
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); }
|
|
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!
|
|
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?
|
|
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!
|
|
Rank: Member Groups: Member
Joined: 5/20/2015 Posts: 11
|
Thx for pointing this out. Works like a charm.
|
|
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!
|
|