C# Close WebView2 problem

Elias 21 Reputation points
2021-06-07T23:11:38.757+00:00

C# Close WebView2 problem

Recently I changed my WebBrowser to WebView2. The WebView2 remains hanging on the desktop on the top-Left corner when closing its container Form.

After exiting the application all the WebView2 instances remain running in the background - they will be hidden but you can still see them in the task manager. Calling the container forms several times will kill the performance and will generate many errors.

To reproduce the problem, all you need to do is to call and close a Form containing a WebView2 several times.

When you close the Form, you will see that the WebView2 is still hanging on the desktop and waiting to be closed manually. There is no way to close it except if you log off from windows or using the TaskManager. Using the task manager you have to kill all the created processes of Microsoft Edge one by one.

Here is the code I am using. I have a simple application with a list of PDF bills. The application will display any selected bill in a PDFViewer Form. The PDFViewer receives the PDF full pathname and it displays it using WebView2. The PDF Viewer works fine but the problem occurs after you close it.

1) The following Form will be displayed to call the PdfViewer

103164-image.png

2) When you click on PDF Preview button, the following code will be called:

        BillPdfViewerForm billPdfViewerForm = new BillPdfViewerForm(this, m_billItem.BillId, strPdfFullPathName);  
        if (billPdfViewerForm.ShowDialog() == DialogResult.OK)  
        {  
           Close();  
        }  

The BillPdfViewerForm is a simple form with a WebView2 that will display the PDF received in strPdfFullPathName. That's all. This is how the PDFViewer will look like after this call:
103126-image.png

Notice that there nothing displayed behind my application you can still see the desktop after the PDFViewer is displayed. But when you close this form, You will see the WebView2 hanging on the desktop at the top left corner

103125-image.png

I can still use my application but each time I close a pdfViewer I get a new WebView2 instance running in the background. Even after closing my application, all created WebView2 Instance will remain running in the background. I can see many Microsoft Edge still running in the the task manager:

103194-image.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,843 questions
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-06-08T02:29:45.35+00:00

    I reproduced this problem, and after my testing, this problem only occurs when we use the Form.ShowDialog() Method.

    Consider modifying it to the Form.Show() method.

            private void button1_Click(object sender, EventArgs e)  
            {  
                Form2 form2 = new Form2(this,@"pdfPath");  
                form2.Show();  
                this.Visible = false;  
            }  
    

    Code in Form2:

            private Form form1;  
            private string pdfPath;  
            public Form2(Form form, string path)  
            {  
                InitializeComponent();  
                form1 = form;  
                pdfPath = path;  
            }  
      
            private void Form2_Load(object sender, EventArgs e)  
            {  
                webView21.Source = new Uri(pdfPath);  
                this.FormClosing += Form2_FormClosing;  
            }  
      
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)  
            {  
                form1.Visible = true;  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.