简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM