Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Performance issue using Multi threading in Net 3.5

Performance issue using Multi threading in Net 3.5

Scheduled Pinned Locked Moved C#
cssdatabaseperformancehelpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KIDYA
    wrote on last edited by
    #1

    Hello All, I'm using Dot Net 3.5 for performing tasks in parallel using below code but It's not showing expected performance i.e. if serial task takes 1 min then using multitasking/parallel tasking it will finish within 1 min or much less time but it takes same time. Code Snippet:

    // Initialize the reset events to keep track of completed threads
    ManualResetEvent[] resetEvents = new ManualResetEvent[arrFileName.Count];

               // Launch each method in it's own thread
               for (int i = 0; i < arrFileName.Count; i++)
               {
                   resetEvents\[i\] = new ManualResetEvent(false);
                   ThreadPool.QueueUserWorkItem(new WaitCallback((object index) =>
                   {
                       int actionIndex = (int)index;
    
                       // Execute the method
                       ThreadExcecute(arrFileName\[actionIndex\]);
    
                       // Tell the calling thread that we're done
                       resetEvents\[actionIndex\].Set();
                   }), i);
               }
    
               // Wait for all threads to execute
               WaitHandle.WaitAll(resetEvents);
    

    Any suggestion?

    A L 2 Replies Last reply
    0
    • K KIDYA

      Hello All, I'm using Dot Net 3.5 for performing tasks in parallel using below code but It's not showing expected performance i.e. if serial task takes 1 min then using multitasking/parallel tasking it will finish within 1 min or much less time but it takes same time. Code Snippet:

      // Initialize the reset events to keep track of completed threads
      ManualResetEvent[] resetEvents = new ManualResetEvent[arrFileName.Count];

                 // Launch each method in it's own thread
                 for (int i = 0; i < arrFileName.Count; i++)
                 {
                     resetEvents\[i\] = new ManualResetEvent(false);
                     ThreadPool.QueueUserWorkItem(new WaitCallback((object index) =>
                     {
                         int actionIndex = (int)index;
      
                         // Execute the method
                         ThreadExcecute(arrFileName\[actionIndex\]);
      
                         // Tell the calling thread that we're done
                         resetEvents\[actionIndex\].Set();
                     }), i);
                 }
      
                 // Wait for all threads to execute
                 WaitHandle.WaitAll(resetEvents);
      

      Any suggestion?

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      KIDYA wrote:

      king it will finish within 1 min or much less time but it takes same time.

      This is not at all necessary. It depends on how much work your processor / memory are involved with and what data is shared between these threads.

      WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

      K 1 Reply Last reply
      0
      • A Abhinav S

        KIDYA wrote:

        king it will finish within 1 min or much less time but it takes same time.

        This is not at all necessary. It depends on how much work your processor / memory are involved with and what data is shared between these threads.

        WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

        K Offline
        K Offline
        KIDYA
        wrote on last edited by
        #3

        Thanks for reply ,that means performance is also based on processor speed/configuration, I'm sharing same dll for performing each task which are not sharing any same inputs,does this matters?

        A 2 Replies Last reply
        0
        • K KIDYA

          Hello All, I'm using Dot Net 3.5 for performing tasks in parallel using below code but It's not showing expected performance i.e. if serial task takes 1 min then using multitasking/parallel tasking it will finish within 1 min or much less time but it takes same time. Code Snippet:

          // Initialize the reset events to keep track of completed threads
          ManualResetEvent[] resetEvents = new ManualResetEvent[arrFileName.Count];

                     // Launch each method in it's own thread
                     for (int i = 0; i < arrFileName.Count; i++)
                     {
                         resetEvents\[i\] = new ManualResetEvent(false);
                         ThreadPool.QueueUserWorkItem(new WaitCallback((object index) =>
                         {
                             int actionIndex = (int)index;
          
                             // Execute the method
                             ThreadExcecute(arrFileName\[actionIndex\]);
          
                             // Tell the calling thread that we're done
                             resetEvents\[actionIndex\].Set();
                         }), i);
                     }
          
                     // Wait for all threads to execute
                     WaitHandle.WaitAll(resetEvents);
          

          Any suggestion?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Threading isn't actually a silver bullet, you know? Anyway it's impossible to tell where this goes wrong without seeing what "ThreadExcecute(arrFileName[actionIndex]);" is doing.

          K 1 Reply Last reply
          0
          • K KIDYA

            Thanks for reply ,that means performance is also based on processor speed/configuration, I'm sharing same dll for performing each task which are not sharing any same inputs,does this matters?

            A Offline
            A Offline
            Abhinav S
            wrote on last edited by
            #5

            Whenever a context switch happens, the operating system performs instructions to save data from one thread and switches over to run a second thread. So yes, even threading will depend on processing capabilities. Threading does not imply processing speed will improve. Threading is useful in scenarios when you are going to perform a background task while you want the user to be involved with something else - for e.g. working on a UI which should be responsive to mouse / keyboard events.

            WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

            1 Reply Last reply
            0
            • L Lost User

              Threading isn't actually a silver bullet, you know? Anyway it's impossible to tell where this goes wrong without seeing what "ThreadExcecute(arrFileName[actionIndex]);" is doing.

              K Offline
              K Offline
              KIDYA
              wrote on last edited by
              #6

              ThreadExcecute function is doing encryption of a files using third party dll. So depending upon file size it will vary the encryption time.But If I've run this in parallel for multiple files then definitely it suppose to take less time.

              L 1 Reply Last reply
              0
              • K KIDYA

                ThreadExcecute function is doing encryption of a files using third party dll. So depending upon file size it will vary the encryption time.But If I've run this in parallel for multiple files then definitely it suppose to take less time.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Maybe.. with an SSD, sure. With a HDD, it's less clear: the throughput isn't that high anyway, and reading many files at the same time can introduce some seek latency.

                K 1 Reply Last reply
                0
                • L Lost User

                  Maybe.. with an SSD, sure. With a HDD, it's less clear: the throughput isn't that high anyway, and reading many files at the same time can introduce some seek latency.

                  K Offline
                  K Offline
                  KIDYA
                  wrote on last edited by
                  #8

                  ok.Thanks,Harold.

                  1 Reply Last reply
                  0
                  • K KIDYA

                    Thanks for reply ,that means performance is also based on processor speed/configuration, I'm sharing same dll for performing each task which are not sharing any same inputs,does this matters?

                    A Offline
                    A Offline
                    Abhinav S
                    wrote on last edited by
                    #9

                    Threading is useful in situations when you want to perform tasks in the background e.g. process complex calculations while allowing the user to navigate across across web page. Threading do not necessarily mean performance will be improved.

                    WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups