Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
Hi @test code ,
The issue is most likely caused by sharing the same user data folder across multiple CoreWebView2Environment creation calls simultaneously, especially when form closing does not wait for pending initialization to finish.
The EnsureCoreWebView2Async method can fail if another WebView2 instance is already trying to initialize with the exact same user data folder parameters. When testing with multiple WebView2 instances, you can wait for the first WebView2 instance to finish initialization so it establishes the environment with the user-data folder. Then the rest of the WebView2 controls can just reuse the shared environment.
Additionally, to prevent the issue from recurring when opening the dialog a second time: ensure the form/dialog gracefully closes and disposes of the WebView2 controls.
Alternative / Workaround: If the application creates a new environment on subsequent form loads while the same user-data folder is reused:
- Try keeping a single globally shared
CoreWebView2Environmentinstance in the application, and use it across all dialogs forEnsureCoreWebView2Async. - Wait for the first WebView2 control on the form to finish initializing, and then initialize the other ones:
Dim environmentOptions As New CoreWebView2EnvironmentOptions()
environmentOptions.AdditionalBrowserArguments = "--disable-web-security"
Dim webViewEnvironment = Await CoreWebView2Environment.CreateAsync(
fixedVersionFolderPath,
userDataFolder,
environmentOptions)
' Initialize the first one and wait for it
Await WebView1.EnsureCoreWebView2Async(webViewEnvironment)
' The environment is now fully established, you can initialize the rest
Await WebView2.EnsureCoreWebView2Async(webViewEnvironment)
Await WebView3.EnsureCoreWebView2Async(webViewEnvironment)
Await WebView4.EnsureCoreWebView2Async(webViewEnvironment)
Await WebView5.EnsureCoreWebView2Async(webViewEnvironment)
If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback.
Thank you.