delphi webbrowser 内存释放问题 100分

delphi webbrowser 内存释放问题 100分,第1张

大概是频刷网页的原因,不好解决.

1、动态创建它

WebBrowser1:= TWebBrowser.Create(Self)

WebBrowser1.Parent:= TWinControl(Self)

每次都把它释放掉,重建以清理内存。还不行就下杀手了。

2、慧握过一段时间自动重启程序。就是再加一个小程序来控制一段时间重启你的程序。

ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW )启动方法

关闭的方法大概是,带岁findwindow来获取你的程序的主窗口,然后向它发送一个退出消息。

上面方法我曾用过,可行。具体使用语法什么的不会就百度一下,相信你能搞定了,不多说前行庆。

webbrowser控件是由IE提供卜岁的,你用了一个webbrowser控件就滚桐相当于启动了一个型备睁IE浏览器,多个webbrowser控件同时使用的话,占用内存是很厉害的。由于IE本身设计上的缺陷,这个占用的内存很难完全释放,只能把整个程序退出才行。所以尽量不要用webbrowser控件数组来运行。

微软不认为这是内存泄露,但我们也得解决问题是不是。有一条思路你可以参考一下:

According to MSDN, The System.Windows.Forms.WebBrowser control is a managed wrapper for the ActiveX WebBrowser control, and uses whichever version of the control is installed on the user's computer.

You can find Dispose(bool) method in metadata of WebBrowser class(Press F12 in Visual Stuio) to release unmanaged resource.(NOT Dispose())

The code here

protected override void Dispose(bool disposing) {

    if (disposing) {

        if (htmlShimManager != null)

        {

            htmlShimManager.Dispose()

        }

        DetachSink()

        ActiveXSite.Dispose()

    }

    base.Dispose(disposing)

}

But if you try to call WebBrowser.Dispose(bool), compiler error CS1540 is shown.

WebBrowser class supports Dispose(bool) method, BUT we can't use that.

I think WebBrowser class was designed by wrong way.

I have a idea to 李或call WebBrowser.Dispose(true).

IT IS VERY SIMPLE! but it's not a good way.

Sample Code in here(3 Buttons, and 1 TextBox need)

using System

using System.Drawing

using System.Runtime.InteropServices

using System.Windows.Forms

namespace Test_20170308_01

{

    public partial class Form1 : Form

    {

        [DllImportAttribute("kernel32.dll"冲激, EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]

        private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize)

        public static void FlushMemory()

        {

            GC.Collect()

            GC.WaitForPendingFinalizers()

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)

            {

                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1)

            }

        }

        public Form1()

        {

            InitializeComponent()

        }

        private void addWeb()

        {

            WebBrowserD webBrowser1 = new WebBrowserD()

            webBrowser1.Size = new Size(1070, 585)

            this.Controls.Add(webBrowser1)

            webBrowser1.Navigate("about:blank")

        }

        private void RemoveWeb()

        {

            foreach (Control ctrl in this.Controls)

            {

          哪判伍      if (ctrl is  WebBrowserD)

                {

                    WebBrowserD web = (WebBrowserD)ctrl

                    this.Controls.Remove(ctrl)

                    web.Navigate("about:blank")

                    web.Dispose(true)

                    FlushMemory()

                }

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {

            addWeb()

        }

        private void button2_Click(object sender, EventArgs e)

        {

            RemoveWeb()

        }

        private void button3_Click(object sender, EventArgs e)

        {

            foreach (Control ctrl in this.Controls)

            {

                if (ctrl is WebBrowserD)

                {

                    WebBrowserD axweb = (WebBrowserD)ctrl

                    axweb.Navigate(textBox1.Text)

                    FlushMemory()

                }

            }

        }

    }

    public class WebBrowserD : WebBrowser

    {

        internal void Dispose(bool disposing)

        {

            // call WebBrower.Dispose(bool)

            base.Dispose(disposing)

        }

    }

}

This code can prevent Memory Leak.

In summary, You need just one class.

    public class WebBrowserD : WebBrowser

    {

        internal void Dispose(bool disposing)

        {

            base.Dispose(disposing)

        }

    }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8183726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存