tryphotino / photino.NET

https://tryphotino.io
Apache License 2.0
911 stars 74 forks source link

SendWebMessage from WindowCreated handler raises a System.AccessViolationException #87

Open albertospelta opened 2 years ago

albertospelta commented 2 years ago

Hi, I've come across this System.AccessViolationException error after trying to send a web message from the WindowCreated handler.

After digging into the issue, I realized that this stems from the fact that the creation of the Photino.Native <ICoreWebView2> _webviewWindow instance is deferred until the message loop is started. Probably due to the Photino::AttachWebView() CreateCoreWebView2EnvironmentWithOptions callback.

Here is the native error

// Exception thrown: read access violation.
this->_webviewWindow.**m_ptr** was nullptr.

To reproduce:

var window = new PhotinoWindow
{
    StartUrl = "wwwroot/main.html",
    WindowCreatedHandler = (sender, args) => (sender as PhotinoWindow).SendWebMessage("test message"),
};
window.WaitForClose();

Expected behavior I would expect all methods to be available in the WindowCreated event handler since the window has already been created

Additional context

Great work on this framework! Thanks, Alberto

ottodobretsberger commented 2 years ago

Thank you for raising this issue. The way this would need to be addressed is to implement a callback event that notifies upon successful creation of the browser, after which the SendWebMessage will be available. If time permits we will look into that as well, unless the community would like to pick up this task.