views:

58

answers:

2

In a previous question I asked how to improve a bit of code. It was said that I should move it to a new thread. I'd never thought about it before so it seems like a great idea to me. So this morning I went ahead and reused a bit of code I already have for processing emails and updated the way I handle image uploads into my site.

So is this a good way to start a new thread and process the images? Is there even a need to lock it like I am?

private static object dummy = new object();

public static void Save(int nProjId, byte[] bData)
{
    var worker = new ThreadStart(() => ProcessImage(nProjId,bData));
    var thread = new Thread(worker);
    thread.Start();
}

private static void ProcessImage(int nProjId, byte[] bData)
{
    lock (dummy)
    {
        try
        {
            byte[] xlargeImage = Thumbs.ResizeImageFile(bData, 700);
            byte[] largeImage = Thumbs.ResizeImageFile(bData, 500);
            //improved based on previous question to use the already reduced image
            byte[] mediumImage = Thumbs.ResizeImageFile(xlargeImage, 200);
            byte[] smallImage = Thumbs.ResizeImageFile(xlargeImage, 100);

            //existing code to actually save the images
            MyGlobals.GetDataAccessComponent().File_Save(
                ConfigurationManager.ConnectionStrings["ImgStore"], 
                nProjId,
                xlargeImage,
                largeImage,
                mediumImage,
                smallImage);
        }
        catch (Exception)
        {
            //ToDo: add error handleing
            { }
            throw;
        }
    }
}

Oh and the images now upload and process nearly instantly (locally) so it's a HUGE help so far. I just want to make sure it's the best way to do it. Oh and I'm using a dual core machine running Server 2008 with 6gb or ram, so I have a little wiggle room to make it faster or use more threads.

+2  A: 

I would suggest using a ThreadPool class, specifically because it will re-use a thread for you rather than you creating a new thread each time, which is a little bit more intensive.

Check out the QueueUserWorkItem method.

Also if you are not using a static resource to write to (I am not sure what exactly File_Save does) I dont think there is a need for your lock. However if you are using a static resource then you should lock just the code that is using it.

Stan R.
I'm not really sure if `File_Save` uses a static resource either. I'm fairly (read **VERY**) green at programing. But to explain it, it is a call to the DAL to actually save the files and some other data to the DB. I'll be moving the file storage to the file system at some point but for now it's easier just left in the DB. But in looking at it, it does not denote static in the DAL, so I dont think it is.
Tim Meers
A: 

Is this for any production code? Or just a sample ? If it is not for production code, apart from using ThreadPool, you can use TPL from .NET4.0 . MS recommends using TPL instead of ThreadPool.

Jagannath
It's for my personal website, so it's "production." But I'm not ready for 4.0 yet, I'm just getting going on using some 3.5 items.
Tim Meers