1

为什么c#控制台应用程序,在循环截取html页面的时候——应用程序占用的内存数量增长了?它工作得越多,音量就越大。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace ConsoleAppScreenShot
{
class Program
{
static String pathApp = System.Windows.Forms.Application.StartupPath.ToString();
static string url = "http://www.ya.ru";
static int width = 960, height = 1380;
static bool isDownloadScreenComplete = false;

[STAThread]
static void Main(string[] args)
{
    Console.WriteLine("hi");

    try
    {
        for (int i = 0; i < 100; i++)
        {
            isDownloadScreenComplete = false;
            RunGenerateScreenshotThread(url, i);

            int number = 0;
            while (!isDownloadScreenComplete) // 5-ти минутный цикл ожидания скрина
            {
                Thread.Sleep(100);
                if (number > 3000)
                    throw new Exception(string.Format(" Ошибка цикла ожидания потока закачки снимка экрана. Ожидание составляет: {0} минут.", (number / 600).ToString()));

                number++;
            }
        }

        Console.ReadKey(true);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Ошибка: {0}", ex.Message);
    }
}

static void RunGenerateScreenshotThread(string url, int count)
{
        var th = new Thread(() =>
        {
            WebBrowser wb = new WebBrowser();
            // Set the size of the WebBrowser control
            wb.Width = width;
            wb.Height = height;

            //wb.DocumentCompleted += GenerateScreenshotCompleted;
            wb.Navigate(url);
            while (wb.ReadyState != WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); }
            try
            {
                // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
                Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
                wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
                wb.Dispose();

                // Save Thumbnail to a File
                bitmap.Save(string.Format("d:\\1\\Screenshot{0}.png", count), System.Drawing.Imaging.ImageFormat.Png);
                bitmap.Dispose();

                count++;
                Console.WriteLine("Natigated to OK {0}", count);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Ошибка: {0}", ex.Message);
            }
            finally
            {
                isDownloadScreenComplete = true;
                Application.ExitThread();   // Stops the thread
            }
        });
        th.SetApartmentState(ApartmentState.STA);
        th.Start();
}
}
}

在一些迭代挂起等待大约 5 分钟后,使用 WebBrowser 流:

while (wb.ReadyState != WebBrowserReadyState.Complete) 
 { System.Windows.Forms.Application.DoEvents(); }

其中状态的值为:

wb.ReadyState == Uninitialized or Interactive

最终返回页面“导航取消” - 或返回它有 5 分钟的延迟。

  1. 如果我的系统是IE8,为什么WebBrowser组件使用IE作为第七版?
  2. 为什么 WebBrowser 组件停止给页面?
  3. 为什么内存量会因应用程序而增加并占用大约 800 mb?

我的任务是使用控制台应用程序将显示的页面保存到硬盘驱动器。也许我们可以以某种方式将pdf格式保存在其中,这样您就可以看到页面的HTML内容。

我很高兴收到任何有价值的评论。

class Program
{
static string url = "http://www.whatbrowser.org";
static int width = 960, height = 1380;
static int count = 0;

[STAThread]
static void Main(string[] args)
{
    Console.WriteLine("hi");

    try
    {
        int number = 1000;
        for (int i = 0; i < number; i++)
        {
            var th = new Thread(obj => RunGenerateScreenshotThread((string)obj));
            th.SetApartmentState(ApartmentState.STA);
            th.Start(url);
            th.Join();

            count++;
        }

        Console.WriteLine("All successfully completed!");
        Console.ReadKey(true);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: {0}", ex.ToString());
    }
}

static void RunGenerateScreenshotThread(string url)
{
    try
    {
        using (WebBrowser wb = new WebBrowser())
        {
            // Set the size of the WebBrowser control
            wb.Width = width;
            wb.Height = height;
            wb.Navigate(url);

            while (wb.ReadyState != WebBrowserReadyState.Complete) 
              { System.Windows.Forms.Application.DoEvents(); }

            if (wb.DocumentTitle == "Navigation Canceled")
                Console.WriteLine("Natigated ERROR {0}", count);
            else
                Console.WriteLine("Natigated OK {0}", count);
            }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: {0}", ex.ToString());
    }
    finally
    {
        Application.ExitThread();   // Stops the thread
    }
}
}
4

0 回答 0