Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 695847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:00:07+00:00 2026-05-14T03:00:07+00:00

I wrote little WPF application with 2 threads – main thread is GUI thread

  • 0

I wrote little WPF application with 2 threads – main thread is GUI thread and another thread is worker.

App has one WPF form with some controls. There is a button, allowing to select directory.
After selecting directory, application scans for .jpg files in that directory and checks if their thumbnails are in hashtable. if they are, it does nothing. else it’s adding their full filenames to queue for worker.

Worker is taking filenames from this queue, loading JPEG images (using WPF’s JpegBitmapDecoder and BitmapFrame), making thumbnails of them (using WPF’s TransformedBitmap) and adding them to hashtable.

Everything works fine, except memory consumption by this application when making thumbnails for big images (like 5000×5000 pixels). I’ve added textboxes on my form to show memory consumption (GC.GetTotalMemory() and Process.GetCurrentProcess().PrivateMemorySize64) and was very surprised, cuz GC.GetTotalMemory() stays close to 1-2 Mbytes, while private memory size constantly grows, especially when loading new image (~ +100Mb per image).

Even after loading all images, making thumbnails of them and freeing original images, private memory size stays at ~700-800Mbytes. My VirtualBox is limited to 512Mb of physical memory and Windows in VirtualBox starts to swap alot to handle this huge memory consumption. I guess I’m doing something wrong, but I don’t know how to investigate this problem, cuz according to GC, allocated memory size is very low.

Attaching code of thumbnail loader class:

class ThumbnailLoader
{
    Hashtable thumbnails;
    Queue<string> taskqueue;
    EventWaitHandle wh;
    Thread[] workers;
    bool stop;
    object locker;
    int width, height, processed, added;

    public ThumbnailLoader()
    {
        int workercount,i;
        wh = new AutoResetEvent(false);
        thumbnails = new Hashtable();
        taskqueue = new Queue<string>();
        stop = false;
        locker = new object();
        width = height = 64;
        processed = added = 0;
        workercount = Environment.ProcessorCount;
        workers=new Thread[workercount];
        for (i = 0; i < workercount; i++) {
            workers[i] = new Thread(Worker);
            workers[i].IsBackground = true;
            workers[i].Priority = ThreadPriority.Highest;
            workers[i].Start();
        }
    }

    public void SetThumbnailSize(int twidth, int theight)
    {
        width = twidth;
        height = theight;
        if (thumbnails.Count!=0) AddTask("#resethash");
    }

    public void GetProgress(out int Added, out int Processed)
    {
        Added = added;
        Processed = processed;
    }

    private void AddTask(string filename)
    {
        lock(locker) {
            taskqueue.Enqueue(filename);
            wh.Set();
            added++;
        }
    }

    private string NextTask()
    {
        lock(locker) {
            if (taskqueue.Count == 0) return null;
            else {
                processed++;
                return taskqueue.Dequeue();
            }
        }
    }

    public static string FileNameToHash(string s)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5");
    }

    public bool GetThumbnail(string filename,out BitmapFrame thumbnail)
    {
        string hash;
        hash = FileNameToHash(filename);
        if (thumbnails.ContainsKey(hash)) {
            thumbnail=(BitmapFrame)thumbnails[hash];
            return true;
        }
        AddTask(filename);
        thumbnail = null;
        return false;
    }

    private BitmapFrame LoadThumbnail(string filename)
    {
        FileStream fs;
        JpegBitmapDecoder bd;
        BitmapFrame oldbf, bf;
        TransformedBitmap tb;
        double scale, dx, dy;
        fs = new FileStream(filename, FileMode.Open);
        bd = new JpegBitmapDecoder(fs, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
        oldbf = bd.Frames[0];
        dx = (double)oldbf.Width / width;
        dy = (double)oldbf.Height / height;
        if (dx > dy) scale = 1 / dx;
        else scale = 1 / dy;
        tb = new TransformedBitmap(oldbf, new ScaleTransform(scale, scale));
        bf = BitmapFrame.Create(tb);
        fs.Close();
        oldbf = null;
        bd = null;
        GC.Collect();
        return bf;
    }

    public void Dispose()
    {
        lock(locker) {
            stop = true;
        }
        AddTask(null);
        foreach (Thread worker in workers) {
            worker.Join();
        }
        wh.Close();
    }

    private void Worker()
    {
        string curtask,hash;
        while (!stop) {
            curtask = NextTask();
            if (curtask == null) wh.WaitOne();
            else {
                if (curtask == "#resethash") thumbnails.Clear();
                else {
                    hash = FileNameToHash(curtask);
                    try {
                        thumbnails[hash] = LoadThumbnail(curtask);
                    }
                    catch {
                        thumbnails[hash] = null;
                    }
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:00:08+00:00Added an answer on May 14, 2026 at 3:00 am

    Solved the problem.
    Just had to replace BitmapCacheOption.OnLoad with BitmapCacheOption.None 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote, my first, WPF application that has a list of items that
I wrote a little app that uploads a selected picture form the ImagePicker to
I wrote a little program that use activemq embedded broker. Program run on one
I wrote an little app in C# to keep track of customers and jobs
I want to write a little application for myself to learn C# and WPF.
I wrote a little console application, and I want it to pause for a
I wrote a little (like 40 lines little) Winforms application with VB.net in Visual
I wrote this little web app that lists the websites running on the local
I wrote a little PyGTK app: Workcycler Now I have the problem that I
I wrote a little application using GAE and the playframework. I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.