thqby / ahk2_lib

MIT License
197 stars 26 forks source link

[WebView2.ahk] In some rare cases WebView2.create(placeholder.Hwnd) returns 0 #47

Closed tomkahn closed 4 months ago

tomkahn commented 5 months ago

Hi thqby,

I am using your WebView2 library to great effect in a script of mine that has a tabbed interface. Each tab has its own WebView.

I create a new WebView like this and insert it into the "WebBrowsers"-array:

placeholder := PopupGUI.Add("Text", "x0 y+0 +Border w" winW)
placeholder.GetPos(, &sizeOfTabControl)
placeholder.Move(,,, winH - sizeOfTabControl +1)
WebBrowsers.InsertAt(index, { plHold : placeholder, view : WebView2.create(placeholder.Hwnd) } )

In some rare cases WebView2.create(placeholder.Hwnd) returns 0 instead of the desired WebView object. Thus whenever I try this:

wb := WebBrowsers[tabCtrl.Value].view.CoreWebView2

I get the error:

This value of type "Integer" has no property named "CoreWebView2".

I can't reproduce the error on my machine, but it has occured multiple times on other people's machines. They run Windows 10.0.19045 (x64). The AutoHotkey version didn't make a difference, it occured with quite a few different versions. I currently use v2.0.11.

What could cause the .create() routine to return 0 instead of the WebView object? Can I prevent that? Or should I just try to create the object again if this happens?

Help from your side would be greatly appreciated.

Warm regards and thank you for publishing this and many other useful libraries! Thomas

thqby commented 5 months ago

WebView2.create has only one return statement and one throw statement. It cannot return 0, I don't know how that happens.

tomkahn commented 4 months ago

Thank you for your quick reply. I think I found the error and you were right: It was not caused by the Create method but rather a concurrency issue in my own code. When switching through tabs in quick succession the problem was that sometimes the WebView of one tab wasn't initialized yet when the script tried to access its properties. Only the WebView of the first tab would get initialized right away, the others only when the user clicks on the specific tab. This isn't clear in the code above.