Why does Task.Factory not do what it's supposed to do?
-
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?
-
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?
-
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.
Okay, so this is a "problem" because it does not give the result I want. Thanks for your reply, I know what to do.
-
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?
"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
-
"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
You are completely right Gerry :-)
-
You are completely right Gerry :-)