The thread does nothing after 2 minutes

I need to grab thumbnails of apsx forms which I can do with the below code. But the problem is that I have about 12000 forms to capture and my code runs for about 2 minutes and grabs as 1500 thumbnails and stops executing without giving any exceptions or errors. Please, help.

    private Bitmap GenerateThumbnail(string Url, int BrowserWidth, int BrowserHeight,    int ThumbnailWidth, int ThumbnailHeight)
    {
        this.Url = Url;
        this.BrowserWidth = BrowserWidth;
        this.BrowserHeight = BrowserHeight;
        this.Height = ThumbnailHeight;
        this.Width = ThumbnailWidth;

        Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();
        return ThumbnailImage;
    }
    private void GenerateThumbnailInteral()
    {
        System.Windows.Forms.WebBrowser webBrowser = new System.Windows.Forms.WebBrowser();
        webBrowser.ScrollBarsEnabled = false;
        webBrowser.Navigate(this.Url);
        webBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
        while (webBrowser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
        {
            System.Windows.Forms.Application.DoEvents();
        }
        webBrowser.Dispose();
    }
    private void WebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
    {
        System.Windows.Forms.WebBrowser webBrowser = (System.Windows.Forms.WebBrowser)sender;
        webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
        webBrowser.ScrollBarsEnabled = false;
        this.ThumbnailImage = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
        webBrowser.BringToFront();
        webBrowser.DrawToBitmap(ThumbnailImage, webBrowser.Bounds);
        this.ThumbnailImage = (Bitmap)ThumbnailImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
        this.ThumbnailImageSmall = (Bitmap)ThumbnailImage.GetThumbnailImage(200, 200, null, IntPtr.Zero);
    }       
    private void SaveImageThumbnail(List<FormClass> objFormClass)
    {
        try
        {
            string url = string.Empty;
            string folderName = string.Empty;
            foreach (FormClass form in objFormClass)//12000 forms
            {
                InternetSetCookie(url, authCookieName, authCookieValue);
                InternetSetCookie(url, "RP_si", rsiCookieValue);                    
                filepath = "D:\\Previews\\";
                folderName = form.FormId.ToString();
                url = string.Format("http://localhost/CRM/Sockets/{0}", form.FileName);
                if (!Directory.Exists(filepath + folderName))
                {
                    Directory.CreateDirectory(filepath + folderName);
                }
                Bitmap image = GenerateThumbnail(url, 1024, 700, 600, 500);
                image.Save(string.Format("{0}{1}/{2}-medium.jpg", filepath, folderName, form.FormId), System.Drawing.Imaging.ImageFormat.Jpeg);                    
            }
        }
         catch (Exception exp)
        {
            Logger.Error("Unhandled Exception", exp);
        }
    }
    public bool SaveThumbnails(string filePath, string authCookieName, string authCookieValue, string rpsiCookieValue)
    {
        this.authCookieName = authCookieName;
        this.authCookieValue = authCookieValue;
        this.rsiCookieValue = rpsiCookieValue;
        this.filepath = filePath;            
        try
        {
            List<FormClass> globalForms = formRepo.GetForms().ToList();//Returns 12000 forms from database
            SaveImageThumbnail(globalForms.ToList());
            return true;
        }
        catch (Exception ex)
        {
            Logger.Error(ex.Message, ex);                
        }
    }

      

From the controller, I call this method as follows.

    public void SaveThumbnailImage(string Guid)
    {
        try
        {
            var formsService = SvcUnityContainer.DefaultContainer.WithStandardMappings().Resolve<IFormsService>();
            new Thread(() => formsService.SaveThumbnails("D:\\Previews\\", FormsAuthentication.FormsCookieName, HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName].Value, HttpContext.Request.Cookies["RP_si"].Value)).Start();
        }
        catch (Exception exp)
        {
            Logger.Error(exp.Message, exp);
        }
    }

      

+3


source to share


1 answer


600 x 500 x 1500 = 450,000,000 pixels, which is 1,350 megabytes of pixel data (3 bytes per pixel).

The process runs out of memory.

Eliminating bitmaps is vital. You will linger after all the memory has been claimed and the garbage collector will run it to free it, but the process must continue after the bits.

Change this:



Bitmap image = GenerateThumbnail(url, 1024, 700, 600, 500);
image.Save(
    string.Format("{0}{1}/{2}-medium.jpg", filepath, folderName, form.FormId),
    System.Drawing.Imaging.ImageFormat.Jpeg);

      

:

using(Bitmap image = GenerateThumbnail(url, 1024, 700, 600, 500))
{
    image.Save(
        string.Format("{0}{1}/{2}-medium.jpg", filepath, folderName, form.FormId),
        System.Drawing.Imaging.ImageFormat.Jpeg);
}

      

On the other hand: you might consider reusing the same web browser, which can help speed up the program. Creating and deleting a web browser 1,500 times in a row can be a waste of time if you can (cleanly) reuse it.

+1


source







All Articles