Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinForms browser has to be removed from Controls before dispose #1574

Closed
MihaMarkic opened this issue Feb 2, 2016 · 4 comments
Closed

WinForms browser has to be removed from Controls before dispose #1574

MihaMarkic opened this issue Feb 2, 2016 · 4 comments

Comments

@MihaMarkic
Copy link

I've found out that when using ChromiumWebBrowser on a (Win)Form, one should remove it from Controls collection before calling browser.Dispose().
If that's not the case then the MDI container or the form (when not a MDI child) will just close. Looks like WM_CLOSE is passed to the main form in such case for some reason. Not sure if this is documented anywhere.

Repro code:

        private ChromiumWebBrowser browser;
        public Form1()
        {
            InitializeComponent();
            browser = new ChromiumWebBrowser("www.google.com")
            {
                Dock = DockStyle.Fill
            };
            Controls.Add(browser);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Controls.Remove(browser);
            browser.Dispose();
        }
@amaitland
Copy link
Member

By default CEF will attempt to close your form, this is known, not sure if it's written down anywhere. You can implement ILifespanHandler.DoClose and ask it politely not to (return true).

https://github.com/cefsharp/CefSharp/blob/master/CefSharp/ILifeSpanHandler.cs#L109

@MihaMarkic
Copy link
Author

Hi Alex,

Do you see any differences in the two approaches? Better/safer to use ILifeSpanHandler?

@amaitland
Copy link
Member

Do you see any differences in the two approaches? Better/safer to use ILifeSpanHandler?

Either should be fine. Typically I'd remove the control or it's parent, then Dispose the control that was removed. e.g. If the ChromiumWebBrowser is on a tab control, remove the tab control, then Dispose of the tab control, the ChromiumWebBrowser instance will be disposed. In general WinForms cleans up after it's self pretty well, it will call Dispose on everything eventually. Much nicer than WPF!

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.WinForms.Example/BrowserForm.cs#L137

The documentation of DoClose is long, if you end up running into problems it's worth a read. https://github.com/cefsharp/CefSharp/blob/master/CefSharp/ILifeSpanHandler.cs#L97

@MihaMarkic
Copy link
Author

Thanks! I'll just clean up normally for now.

dpbackes added a commit to dpbackes/CefSharp that referenced this issue Aug 2, 2016
- Make it so that closing a tab in the WinForms example does not close the
  entire application. We need to make sure that the LifeSpanHandler is
  still around when `managedCefBrowserAdapter` is disposed since it is
  going to call `CloseBrowser`. That way, we can implement LifeSpanHandler
  and reject closing the entire application as described in (this
  issue)[cefsharp#1574]
dpbackes added a commit to dpbackes/CefSharp that referenced this issue Aug 2, 2016
- Make it so that closing a tab in the WinForms example does not close the
  entire application. We need to make sure that the LifeSpanHandler is
  still around when `managedCefBrowserAdapter` is disposed since it is
  going to call `CloseBrowser`. That way, we can implement LifeSpanHandler
  and reject closing the entire application as described in cefsharp#1574
dpbackes added a commit to dpbackes/CefSharp that referenced this issue Aug 3, 2016
- Make it so that the lifespan handler is still called when the
  ChromiumWebBrowser is disposed We need to make sure that the
  LifeSpanHandler is still around when `managedCefBrowserAdapter`
  is disposed since it is going to call `CloseBrowser`. That way,
  we can implement LifeSpanHandler and reject closing the entire
  application as described in cefsharp#1574
amaitland pushed a commit that referenced this issue Aug 9, 2016
- Make it so that the lifespan handler is still called when the
  ChromiumWebBrowser is disposed We need to make sure that the
  LifeSpanHandler is still around when `managedCefBrowserAdapter`
  is disposed since it is going to call `CloseBrowser`. That way,
  we can implement LifeSpanHandler and reject closing the entire
  application as described in #1574
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants