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. C# regarding running multiple task

C# regarding running multiple task

Scheduled Pinned Locked Moved C#
csharptutorialquestion
5 Posts 5 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.
  • M Offline
    M Offline
    Mou_kol
    wrote on last edited by
    #1

    State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks

    var stage1 = Task.Run(() =>
    {

     });
        
     var stage2 = Task.Run(() =>
     {
            
     });
        
     // Block until both tasks have completed.
     // This makes this method prone to deadlocking.
     // Consider using 'await Task.WhenAll' instead.
     Task.WaitAll(stage1, stage2);
    
    OriginalGriffO L L P 4 Replies Last reply
    0
    • M Mou_kol

      State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks

      var stage1 = Task.Run(() =>
      {

       });
          
       var stage2 = Task.Run(() =>
       {
              
       });
          
       // Block until both tasks have completed.
       // This makes this method prone to deadlocking.
       // Consider using 'await Task.WhenAll' instead.
       Task.WaitAll(stage1, stage2);
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Mou_kol wrote:

      State1 & state2 will run parallel or State1 will run first and later State2 will run ?

      Yes. Or perhaps 2 will run first and 1 later. There is no way to control what happens with tasks of equal priority: they exist on separate threads, and will be executed when the OS decides it is appropriate , and as a core becomes available. That means that the execution order of the tasks is indeterminate: it cannot be predicted or relied upon.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • M Mou_kol

        State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks

        var stage1 = Task.Run(() =>
        {

         });
            
         var stage2 = Task.Run(() =>
         {
                
         });
            
         // Block until both tasks have completed.
         // This makes this method prone to deadlocking.
         // Consider using 'await Task.WhenAll' instead.
         Task.WaitAll(stage1, stage2);
        
        L Offline
        L Offline
        lmoelleb
        wrote on last edited by
        #3

        Task.Run will execute the contained code on the ThreadPool. The TreadPool will allow a number of tasks to run in parallel. So as long as you have not reached this limit and there is a CPU core available, they will execute in parallel. If you have reached the limit, they might run one by one - and they might delay execution until a thread becomes available. This is typically not something you need to think about, but worth knowing if you have long running operations or if you are queueing a lot of operations on the thread pool - for example looping over a collection and running something for each item. If you need to do the latter, look into "Task Parallel Library"[^]

        1 Reply Last reply
        0
        • M Mou_kol

          State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks

          var stage1 = Task.Run(() =>
          {

           });
              
           var stage2 = Task.Run(() =>
           {
                  
           });
              
           // Block until both tasks have completed.
           // This makes this method prone to deadlocking.
           // Consider using 'await Task.WhenAll' instead.
           Task.WaitAll(stage1, stage2);
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Ask yourself: Why am I doing this?

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • M Mou_kol

            State1 & state2 will run parallel or State1 will run first and later State2 will run ? please guide me. thanks

            var stage1 = Task.Run(() =>
            {

             });
                
             var stage2 = Task.Run(() =>
             {
                    
             });
                
             // Block until both tasks have completed.
             // This makes this method prone to deadlocking.
             // Consider using 'await Task.WhenAll' instead.
             Task.WaitAll(stage1, stage2);
            
            P Offline
            P Offline
            primem0ver
            wrote on last edited by
            #5

            If I were you and for some reason I needed to read/write multiple files in parallel (though Gerry is right when it comes to writing files to traditional hard disk drives), instead of the methods you use, I would simplify things by using the Parallel.For or the Parallel.Foreach method.

            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