繁体   English   中英

Web浏览器控件-释放应用程序的资源

[英]webbrowser control - releasing resources of an application

我的问题是我创建了一个具有webrowser控件的简单应用程序。 它每20秒导航到一个新站点。 经过一定数量的导航后,应用程序占用的内存增加了。 我试图处置,删除并再次创建webrouser控件,但未能成功。 释放应用程序所有资源的唯一方法是重新启动它。 我花了很多精力来解决这个问题。 请给我一些提示或链接,以便我阅读。 提前致谢。

实际的代码比演示的代码大得多,但是想法是相同的。 代码如下:

for (int i = 0; i < 100000; i++)
{
    webBrowser1.Navigate("http://stackoverflow.com");
    Wait(20000);
}

方法定义:

private void Wait(long value)
{
    Stopwatch sw = new Stopwatch();
    sw.Start();

    while (sw.ElapsedMilliseconds < value)
        Application.DoEvents();
}

现在不存在问题,因为可以使用另一个名为WebKitBrowser的浏览器控件找到解决方案。 感谢所有试图帮助我的人。 这是我在这个引人入胜的网站上的第一个问题。 我很喜欢它。

您正在阻塞主UI线程消息循环,并使用Application.DoEvents ,这是很糟糕的!

代替此,您可以使用像这样的System.Windows.Forms.Timer

class Form1 : Form
{
  Timer _Timer;
  int _Index = 0;

  public Form1()
  {
    _Timer = new Timer { Enabled = true, Interval = 20000 };
    _Timer.Tick += ( s, e ) => TimerTick();
  }

  void TimerTick()
  {
     if ( _Index >= 100000 )
     {
       _Timer.Dispose();
       _Timer = null;
       return;
     }

     webBrowser.Navigate( ... );

     _Index++;
   }

我猜这将解决您的问题。 无论如何,这是值得做的。

问题仍然存在。 我尝试使用下面链接中显示的方法,但未成功。 如何解决IE WebBrowser控件中的内存泄漏?

它会暂时减少内存,然后再次大幅增加。 如此循环。 这是修改后的代码:

  public partial class Form1 : Form
   {

       [DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
       internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

       [DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
       internal static extern IntPtr GetCurrentProcess();



      private void button1_Click(object sender, EventArgs e)
        {

           for (int i = 0; i < 100000; i++)
              {
                 webBrowser1.Navigate("http://stackoverflow.com");
                 while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();

                // anytime when I want to reduce the occupied memory I do this
                IntPtr pHandle = GetCurrentProcess();
                SetProcessWorkingSetSize(pHandle, -1, -1);
              }
        }



   }

我使用WebKit浏览器解决了问题。 这里是那些也有兴趣使用它的链接: http : //webkitdotnet.sourceforge.net/basics.php?p=4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM