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

You must login to ask a question.

Forgot Password?

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:22:04+00:00 2026-06-15T09:22:04+00:00

I was looking for this and I couldn’t figure how to do it. I

  • 0

I was looking for this and I couldn’t figure how to do it.

I have a some threats (tasks) in an App.

    foreach (string targetMachine in targetMachines)
    {
        Task<int> task = Task.Run(() => Magic(targetMachine));
    }

I created a task for every object in an array.
The task returns a value.
I want to process the values and act based on them. As example, if task return 0 dont do anything, if returns 1 write a log, if returns 2 run a process.

How can I acoomplish this?
If I process the return values inside the foreach:

foreach (string targetMachine in targetMachines)
    {
        Task<int> task = Task.Run(() => Magic(targetMachine));
        Task.Waitforexit()
        if (task.result == 2)
        {
        do something
        }
    }

I think, task are not going to be useful and the programa will wait each task to be completed to continue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:22:05+00:00Added an answer on June 15, 2026 at 9:22 am

    It can be something like this:

    foreach (string targetMachine in targetMachines)
    {
        Task.Run(() =>
        {
            var result = Magic(targetMachine);
            if (result == 2)
            {
                DoSomething();
            }
        });
    }
    

    OR (using async/await)

    foreach (string targetMachine in targetMachines)
    {
        var result = await Task.Run(() => Magic(targetMachine));
        if (result == 2)
        {
            DoSomething();
        }
    }
    

    OR (using ContinueWith)

    foreach (string targetMachine in targetMachines)
    {
        Task<int>.Run(()=>Magic(targetMachine))
            .ContinueWith(t =>
            {
                if (t.Result == 2)
                {
                    DoSomething();
                }
            });
    }
    

    If you want to wait to finish all of your tasks

    Parallel.ForEach(targetMachines, targetMachine =>
    {
        var result = Magic(targetMachine);
        if (result == 2)
        {
            DoSomething();
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for some others thoughts on this cause I couldn't come up with anything
I was looking at http://wmd-editor.com/ but couldn't figure out how to use this in
I have been looking for a solution for this, but couldn't find any. I
I have been looking around for this but couldn't find an answer anywhere, so
Was looking for the answer on the net, but couldn't find anything. This little
I have been looking for an answer to this and could not find it
I have been looking everywhere but could not find a tutorial for this. I
Looking at this article: http://www.galloway.me.uk/tutorials/singleton-classes/ I have implemented a singleton, my question is the
I was looking into this issue but I couldn't find any solid answers for
I was looking for but couldn't find something like this. I need to create

Explore

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

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.