question

Elias-0405 avatar image
0 Votes"
Elias-0405 asked TimonYang-MSFT edited

C# Close WebView2 problem

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


dotnet-csharp
image.png (238.7 KiB)
image.png (170.0 KiB)
image.png (47.1 KiB)
image.png (332.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Elias-0405
I have not heard from you for a couple of days.
If there are still some issues with this code, please let me know and we can continue to improve it.

0 Votes 0 ·

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT edited

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the quick answer and the workaround that you pointed out which is very similar to the one we are using here.

Basically, we hide the WebView2 on FormClosed event

   private void BillPdfViewerForm_FormClosed(object sender, FormClosedEventArgs e)
   {
      pdfWebView2.Hide();
   }


The problem with that is we hide the WebView2 while it is still running in background. The workaround we are using can be a temporary fix but we are still looking for a real solution. The WebView2 need to be closed after closing its parent form. Notice that sometimes we get a WebView2 error message when starting/closing our application many times.

Regards

0 Votes 0 ·

@Elias-0405
When I used Show() instead of ShowDialog(), those processes disappeared as Form2 was closed.
Moreover, the current phenomenon is not normal and does not meet expectations. I suggest you raise a question in the GitHub repository of webview2 and ask them to check this issue.
If it is an error, you can ask them to fix it. If it is normal, you can ask them to explain the reason for this design.

0 Votes 0 ·