Question Dispose webView2

philip

Member
Joined
Mar 23, 2024
Messages
6
Programming Experience
1-3
Hi good day! I'm displaying a pdf from local machine using webView2 from Microsoft.Web.WebView2 by Microsoft installed through Nuget Packages. Now I'm really confused should I manually dispose webView2? but it says that webView2 is managed resource base on chatGPT. I just want to make sure that the value put in the webView.Source is clean/managed when pdf is no longer displayed. Thanks in advance.
Initialize webView.Source to pdf file in local machine:
                            try {

                                webView.Source = new Uri(tempFilePath); // set the source of webView to the temp pdf path
                                webView.Visible = true; // set the webView to visible                                                                                       

                                //Path of temporary file for search result
                                MessageBox.Show(tempFilePath, "PDF INVENTORY TRIAL search funct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "PDF INVENTORY TRIAL search funct", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
 
Last edited:
If you are hosting the WebView2 control following the normal conventions or practices, it will be disposed when the hosting outerContainerUI component is closed and disposed. So if you used the WinForms designer, or you used vanilla XAML to insert the control into your UI, the disposing will be done properly at the correct time. If you are dynamically creating the control, and not properly hooking it up to the outer UI, then you will have to manually dispose it.
 
If you are hosting the WebView2 control following the normal conventions or practices, it will be disposed when the hosting outerContainerUI component is closed and disposed. So if you used the WinForms designer, or you used vanilla XAML to insert the control into your UI, the disposing will be done properly at the correct time. If you are dynamically creating the control, and not properly hooking it up to the outer UI, then you will have to manually dispose it.

I used the winForm where I put the webView2. Do you mean when I close the winform the webView2 used resources is handled and no need to call Dispose() manually? Sorry for the follow up question I'm making sure I understand what you said, although I read 30x times already your answer and done some research.
 
Last edited:
All controls need to be disposed and any controls you add to a form in the designer will be disposed when that form is disposed. If you added this WebView2 to the form in the designer then you don't need to explicitly dispose it, any more than you need to explicitly dispose a Button or a TextBox. That's basically what post #2 is saying.
 
All controls need to be disposed and any controls you add to a form in the designer will be disposed when that form is disposed. If you added this WebView2 to the form in the designer then you don't need to explicitly dispose it, any more than you need to explicitly dispose a Button or a TextBox. That's basically what post #2 is saying.

Thanks for the clarification!
 
Back
Top Bottom