Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question: Largest Hub for Question Solutions and Expert Answers

Querify Question: Largest Hub for Question Solutions and Expert Answers Logo Querify Question: Largest Hub for Question Solutions and Expert Answers Logo

Querify Question: Largest Hub for Question Solutions and Expert Answers Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 784035

Querify Question: Largest Hub for Question Solutions and Expert Answers Latest Articles

admin
  • 138
adminEnlightened
Asked: July 14, 20242024-07-14T05:08:28+00:00 2024-07-14T05:08:28+00:00

different task approaches work differently duplicate

  • 138
This question already has answers here:

Captured variable in a loop in C# (11 answers)
Regarding local variable passing in Thread (2 answers)
Closed 3 years ago.

I would like to ask why this two approaches are different and returns two different set of values:

The first one which is correct in my opinion return values from 0 to 8 and works on different threads (LINQPad code):

void Main() {     var newTasks = Enumerable.Range(0, 9).Select(x => Task.Run(() => DoSomething(x)));      Task.WhenAll(newTasks); }      public int DoSomething(int value) {     return value; } 

Second, which is not correct in my opinion which returns random values a specially 9 but also works on different threads.

void Main() {     var tasks = new List<Task<int>>();          for (var index = 0; index < 9; index++)     {         var task = Task.Run(() => DoSomething(index));                  tasks.Add(task);     }          Task.WaitAll(tasks.ToArray()); }      public int DoSomething(int value) {     return value; } 

Is it possible to modify the second one and get the result similar to first example?

Thanks for your answers.

Related Posts
  • Related Link: how to drag and drop uibutton title to another button title but the original button remain in same place objective c?
asynchronouscmultithreadingnettask
  • 0 0 Answers
  • 1k Views
  • 0 Followers
  • 1k
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 1mil
  • Answer 1
  • Best Answers 0
  • Users 40k
  • Popular
  • Answers
  • admin

    vba code excel range not pasting to outlook properly

    • 1 Answer
  • MilaBrext

    Заказать Хавал - только у нас вы найдете цены ниже ...

    • 0 Answers
  • admin

    Creating a Regex to Match the First N Characters of ...

    • 0 Answers
  • Reda
    Reda added an answer Convert Range to Text February 27, 2025 at 8:30 pm

Top Members

admin

admin

  • 1mil Questions
  • 855 Points
Enlightened
Reda

Reda

  • 0 Questions
  • 22 Points
Begginer
Aria Carter

Aria Carter

  • 0 Questions
  • 21 Points
Begginer

Trending Tags

android azuredevops azurepipelines c database debugging decimal docker html ios java javascript jquery php python reactjs regex rubyonrails sql vlc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question: Largest Hub for Question Solutions and Expert Answers

Querify Question: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.