Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get return value of method in parallel execution

I am using Parallel.Invoke to execute single method with different input values, but I want to get return value of the method. How can I get it ?

public class Work
{
    public static void Main()
    {
        Parallel.Invoke(() => DoWork("Raju"),
                        () => DoWork("Ramu"));
    }

    public static string DoWork(string data)
    {
        return "testing" + data;
    }
}

In above method I want get DoWork return value.

like image 802
Raj Discussion Avatar asked Mar 23 '15 13:03

Raj Discussion


1 Answers

Just handle the return value like this:

string result1, result2;

Parallel.Invoke(() => result1 = DoWork("Raju"),
                () => result2 = DoWork("Ramu"));

Also remember that whenever you do something in parallel you need to be careful to avoid data races and race conditions.

like image 143
Simon Karlsson Avatar answered Oct 02 '22 05:10

Simon Karlsson