Link to home
Start Free TrialLog in
Avatar of DuNuNuBatman
DuNuNuBatman

asked on

Multiple Threads in C#

I am working on a class library that uses methods to create new threads that fire events when they are completed. Here's how it works.
1. Application calls method ResizeImageAsync(inputFile, outputFile, width, height);
2. The ResizeImageAsync() creates a new thread that runs in low priority with the IsBackground property = true.
3. The thread resizes the image and writes it to a folder based on the outputFile parameter.
3. The thread calls an event called OnResize(outputFile). The outputFile parameter is the value of the ResizeImageAsync() outputFile parameter.

The problem I am having is the ResizeImageAsync() method will be called multiple times at once. When I call it more than 30 times the computer hangs. I think it's because I am creating too many threads at once.
I haven't done very much multi-threading yet for an application so I'm not sure of which System.Threading class I should use to pool the threads so they are managed properly. I could just be disposing of the threads improperly as well.

I've attached some of the code in the application.
public event ResizeCompletedDelegate OnResize;
 
        public void ResizeImageMaxAsync(string imagePath, string outputPath, int maxWidth, int maxHeight)
        {
            Thread thread = new Thread(delegate()
                                           {
                                               this.ResizeImageMax(imagePath, outputPath, maxWidth, maxHeight);
 
                                               // Fire the completed event
                                               this.InvokeOnResize(outputPath);
                                           });
            thread.Priority = ThreadPriority.Lowest;
            thread.IsBackground = true;
            thread.Start();
        }
 
        private void InvokeOnResize(string filePath)
        {
            ResizeCompletedDelegate resizeDelegate = this.OnResize;
            if (resizeDelegate != null)
            {
                resizeDelegate(filePath);
            }
        }
 
 
// Application Usage Section.
 
// This is the event that will fire. It just writes out the path to the location of the resized image.
editor.OnResize += delegate(string outputFile)
        {
            Console.WriteLine("File: {0} Completed Resize", outputFile);
        };
 
// This is just an example of looping through multiple instances of the ResizeImageAbsoluteAsync method. It returns immediatelly after the thread is created and not when the thread has finished.
for (int i = 0; i < 60; i++)
{
    editor.ResizeImageAbsoluteAsync(imagePath, String.Format(outputPath, Guid.NewGuid().ToString()), 250, 250);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DuNuNuBatman
DuNuNuBatman

ASKER

Also, is there a way I can limit the number of threads executed at one time. Ideally, it should be based on the number of cores the machine has because I don't want to be resizing 20 images at once. I want them to start returning as soon as possible.
You can limit the number of threads using the SetMaxThreads() method of the ThreadPool class.
It doesn't look like I can set the thread priority using the ThreadPool class. Do you think creating a thread in the thread pool would be an issue?
I've been running it and it seems to be working correctly, but I don't know if that could cause problems.
Thread thread = new Thread(delegate()
                                                        {
                                                            this.ResizeImageMax(imagePath, outputPath, maxWidth, maxHeight);
 
                                                            // Fire the completed event
                                                            this.InvokeOnResize(outputPath);
                                                        });

Open in new window

Well as far as I know you cannot set the thread priority when you are using ThreadPool but this class provides an efficient thread management functionality. If you use the approach you are using right now you will have to limit the number of threads created at a particular instance of time. But this functionality is already built into ThreadPool and since it is a framework class I think it is trouble free.