Andrew Simpson
Andrew Simpson

Reputation: 7324

Proper usage of Parallel processing

I have a C# desktop application framework 4.5.

at the moment i am using a system,timer to upload images to my web server (as quickly and efficiently as possible.

This is that code:

    void tmrFeeder_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            tmrFeeder.Stop();
            MotionFrame motion = GetNextUpload();
            if (motion != null && motion.Frame != null)
            {
                request.UploadMotion(motion.Frame, Shared.Alias, motion.camIndex, motion.MessageLog);
            }
        }
        catch (Exception ex)
        {
            evError();
        }
        finally
        {
            if (!stop)
            {
                tmrFeeder.Start();
            }
        }
    }

I was wondering that if I have a multi-core processor whether I could take advantage of parallel processing.

But in my scenario above the sequential order of the images being uploaded is of paramount importance.

So, my understanding of parallel tasks would break that/my rule.

However, I was drawn to this because TPL tells me that it utilizes the hardware architecture of the processor in its most efficient manner and load balances the processing over the available CPU cores.

Is there anyway to take advantage and improve the efficiency of my code without losing the sequential order of my images?

Thanks

Upvotes: 0

Views: 80

Answers (1)

Samir Hafez
Samir Hafez

Reputation: 214

If you care about the upload order I can't see how the TPL would be of any benefit to you.

It "utilizes the hardware ... and load balances the processing over the available CPU cores". True, on tasks that can run in parallel. Your task cannot.

You would just allocate a bunch of threads that do nothing but wait in line for their turn.

Upvotes: 1

Related Questions