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. .NET (Core and Framework)
  4. Why does Task.Factory not do what it's supposed to do?

Why does Task.Factory not do what it's supposed to do?

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionasp-netarchitecture
6 Posts 2 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.
  • W Offline
    W Offline
    Wiep Corbier
    wrote on last edited by
    #1

    I have an eight core computer and therefor I can have a tasklist with 8 tasks. But not al 8 task are perform correctly or something else doesn't work. My objective: I want 8 count results be shown on a website (MVC) so I have 8 different methods that get the 8 result seperatly. One method is like this:

        protected void GetArticleCount()
        {
            ArticlesManager articlesManager = new ArticlesManager();
            ViewBag.ArticlesCount = articlesManager.ArticleCount();
        }
    

    And so there are 7 more. My code to collect all 8 is like this:

            var t1 = Task.Factory.StartNew(() => GetArticleCount());
            var t2 = Task.Factory.StartNew(() => GetBrancheCount());
            var t3 = Task.Factory.StartNew(() => GetCompaniesCount());
            var t4 = Task.Factory.StartNew(() => GetDeliveriesCount());
            var t5 = Task.Factory.StartNew(() => GetDistributionCentresCount());
            var t6 = Task.Factory.StartNew(() => GetDocksCount());
            var t7 = Task.Factory.StartNew(() => GetInvoicesCount());
            var t8 = Task.Factory.StartNew(() => GetOrdersCount());
    
            var taskList = new List { t1, t2, t3, t4, t5, t6, t7, t8 };
            Task.WaitAll(taskList.ToArray());
    

    When I run it 5 off the 8 are visible on my website and the remaining 3 are also shown after a refresh (F5). When I run the code without the "Task.Factory" but sequentially, all 8 are shown immediately. Question: why? What's wrong with my code?

    L 2 Replies Last reply
    0
    • W Wiep Corbier

      I have an eight core computer and therefor I can have a tasklist with 8 tasks. But not al 8 task are perform correctly or something else doesn't work. My objective: I want 8 count results be shown on a website (MVC) so I have 8 different methods that get the 8 result seperatly. One method is like this:

          protected void GetArticleCount()
          {
              ArticlesManager articlesManager = new ArticlesManager();
              ViewBag.ArticlesCount = articlesManager.ArticleCount();
          }
      

      And so there are 7 more. My code to collect all 8 is like this:

              var t1 = Task.Factory.StartNew(() => GetArticleCount());
              var t2 = Task.Factory.StartNew(() => GetBrancheCount());
              var t3 = Task.Factory.StartNew(() => GetCompaniesCount());
              var t4 = Task.Factory.StartNew(() => GetDeliveriesCount());
              var t5 = Task.Factory.StartNew(() => GetDistributionCentresCount());
              var t6 = Task.Factory.StartNew(() => GetDocksCount());
              var t7 = Task.Factory.StartNew(() => GetInvoicesCount());
              var t8 = Task.Factory.StartNew(() => GetOrdersCount());
      
              var taskList = new List { t1, t2, t3, t4, t5, t6, t7, t8 };
              Task.WaitAll(taskList.ToArray());
      

      When I run it 5 off the 8 are visible on my website and the remaining 3 are also shown after a refresh (F5). When I run the code without the "Task.Factory" but sequentially, all 8 are shown immediately. Question: why? What's wrong with my code?

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

      Nothing is wrong. But you need to understand that starting a new task does not automatically start it immediately in a new core. The operating system queues tasks for processing them and starts them when resources are available.

      W 1 Reply Last reply
      0
      • L Lost User

        Nothing is wrong. But you need to understand that starting a new task does not automatically start it immediately in a new core. The operating system queues tasks for processing them and starts them when resources are available.

        W Offline
        W Offline
        Wiep Corbier
        wrote on last edited by
        #3

        Okay, so this is a "problem" because it does not give the result I want. Thanks for your reply, I know what to do.

        1 Reply Last reply
        0
        • W Wiep Corbier

          I have an eight core computer and therefor I can have a tasklist with 8 tasks. But not al 8 task are perform correctly or something else doesn't work. My objective: I want 8 count results be shown on a website (MVC) so I have 8 different methods that get the 8 result seperatly. One method is like this:

              protected void GetArticleCount()
              {
                  ArticlesManager articlesManager = new ArticlesManager();
                  ViewBag.ArticlesCount = articlesManager.ArticleCount();
              }
          

          And so there are 7 more. My code to collect all 8 is like this:

                  var t1 = Task.Factory.StartNew(() => GetArticleCount());
                  var t2 = Task.Factory.StartNew(() => GetBrancheCount());
                  var t3 = Task.Factory.StartNew(() => GetCompaniesCount());
                  var t4 = Task.Factory.StartNew(() => GetDeliveriesCount());
                  var t5 = Task.Factory.StartNew(() => GetDistributionCentresCount());
                  var t6 = Task.Factory.StartNew(() => GetDocksCount());
                  var t7 = Task.Factory.StartNew(() => GetInvoicesCount());
                  var t8 = Task.Factory.StartNew(() => GetOrdersCount());
          
                  var taskList = new List { t1, t2, t3, t4, t5, t6, t7, t8 };
                  Task.WaitAll(taskList.ToArray());
          

          When I run it 5 off the 8 are visible on my website and the remaining 3 are also shown after a refresh (F5). When I run the code without the "Task.Factory" but sequentially, all 8 are shown immediately. Question: why? What's wrong with my code?

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

          "Refresh rate" is not necessarrily tied to when your tasks are finishing. I would use something "other" to determine when tasks actually finish than when it "shows up". Your "tasks" look like queries; some will probably always run last. You should see a pattern. Even too many (redundant) refreshes can affect overall performance (flicker; bouncing cursor).

          "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

          W 1 Reply Last reply
          0
          • L Lost User

            "Refresh rate" is not necessarrily tied to when your tasks are finishing. I would use something "other" to determine when tasks actually finish than when it "shows up". Your "tasks" look like queries; some will probably always run last. You should see a pattern. Even too many (redundant) refreshes can affect overall performance (flicker; bouncing cursor).

            "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

            W Offline
            W Offline
            Wiep Corbier
            wrote on last edited by
            #5

            You are completely right Gerry :-)

            L 1 Reply Last reply
            0
            • W Wiep Corbier

              You are completely right Gerry :-)

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

              Trace.WriteLine() and a StopWatch help me with timings...

              "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

              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