欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 如何快速关闭 CefSharp.WinForms ;如何快速关闭 CefSharp;

程序员文章站 2022-06-08 17:58:51
...

C# 如何快速关闭 CefSharp.WinForms ;如何快速关闭 CefSharp;

直接杀死进程 CefSharp.BrowserSubprocess 再释放资源

 cefBrowser.JavascriptObjectRepository.UnRegisterAll();//解绑对象 高版本才有
 cefBrowser.CloseDevTools();//关闭调试
 cefBrowser.GetBrowser().CloseBrowser(false);//关闭浏览器

KillBrowserSubprocess();//杀死进程
CefSharp.Cef.Shutdown();//释放资源


/// <summary>
        /// 清理CefSharp.BrowserSubprocess
        /// </summary>
        private void KillBrowserSubprocess()
        {
            try
            {
                string SYSPath = System.AppDomain.CurrentDomain.BaseDirectory;
                Process[] processs = Process.GetProcessesByName("CefSharp.BrowserSubprocess");
                if (processs != null && processs.Length > 0)
                {
                    for (int i = 0; i < processs.Length; i++)
                    {
                        Process process = processs[i];

                        bool bKill = false;
                        if (process.MainModule != null)
                        {
                            string FileName = process.MainModule.FileName;
                            if (SYSPath.Contains(FileName) || FileName.Contains(SYSPath))
                            {
                                bKill = true;
                            }
                        }
                        if (bKill)
                        {
                            process.Kill();
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "清理CefSharp.BrowserSubprocess异常");
            }

        }