My C# application uses WebView2.
It is required that multiple instances are open at the same time which do not share sessions. According to this explanation of the WebView2 process model, this is achieved by using different UserDataFolders, passed at creation of the CoreWebView2Environment.
The app is currently loaded from a read-only network share, so the default setting to create the user data folders alongside the exe is not eligible, so my implementation creates different UserDataFolders in the user temp directory.
To clean up, I would like to delete the created directories when the application is closed. The documentation suggests the BrowserProcessExited Event which should be called when all resources taken by the WebView2 are released.
But the BrowserProcessExited event never gets called.
In the Page where the WebView2 is used, I do:
public void MyApp_Closing(object sender, CancelEventArgs e)
{
glucoTabWebcontrol2.CoreWebView2.Environment.BrowserProcessExited += Environment_BrowserProcessExited;
}
// This is never called
private void Environment_BrowserProcessExited(object sender, CoreWebView2BrowserProcessExitedEventArgs e)
{
try
{
System.IO.Directory.Delete(((CoreWebView2Environment)sender).UserDataFolder);
} catch (Exception ex)
{
... handle exception
}
}
My guess is that the application is closed before the event gets fired.
What is necessary to achieve that the BrowserProcessExited
event is received?