Get the return value of a method in parallel execution C # .Net4.0

I use Parallel to execute a single method with different input values, but I want to get the 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 "tesing" + data; } } 

In the above method, I want to get the DoWork return value.

+6
source share
1 answer

Just handle the return value as follows:

 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.

+12
source

Source: https://habr.com/ru/post/984133/


All Articles