Asynchronous Listing Element

I have a problem in Asp.net C #, .net 3.5

I have the code below:

List<object> objectList = new List<object>(); foreach(var item in listItem) { object obj = getData (item); objectList.add(obj); } Console.Write("Finish all"); 

Every time getData (item); triggered getData (item); It takes about 1 s;

I want all the elements in the ListItem executed simultaneously (here getData(item) ), and then executed after the foreach finishes Console.write("Finish all") .

How can i do this? Any idea would be appreciated!

+4
source share
3 answers

You can use ThreadPool and ManualResetEvent :

 int itemCount = listItem.Length; List<object> objectList = new List<object>(); ManualResetEvent[] resetEvents = new ManualResetEvent[itemCount]; for (int i = 0; i < itemCount; i++) { var item = listItem[i]; resetEvents[i] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback((object index) => { object obj = getData(item); lock (objectList) objectList.add(obj); resetEvents[(int)index].Set(); }), i); } WaitHandle.WaitAll(resetEvents); Console.Write("Finish all"); 

ThreadPool will run the task in the background, and the current thread will wait for all jobs to finish using ManualResetEvent.

+2
source

Use Parallel.ForEach / For to execute getData for all elements asynchronously. Paralle.ForEach / To return not until all iterations have been completed.

Now, when you add a comment about using .net 3.5, my answer may be skipped.

+1
source

Hi, just run this Console application code and compare the results using foreach and Parallel.Foreach. In principle, Parallel is useful in tasks with a long lead time or in tasks that consume too much time.

 using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { List<object> objectList = new List<object>(); var listItem = new List<Person> { new Person { Id = 1, Name = "Person 1" }, new Person { Id = 2, Name = "Person 2" }, new Person { Id = 3, Name = "Person 3" } , new Person { Id = 4, Name = "Person 4" } }; var start = DateTime.Now; var parallelList = listItem; //using foreach foreach (var item in listItem) { object obj = getData(item); objectList.Add(obj); } var end = DateTime.Now.Subtract(start).TotalSeconds; Console.Write("\nUsing foreach...Finish all in " + end + " second \n"); start = DateTime.Now; //using Parallel Parallel.ForEach(parallelList, item => { object obj = getData(item); objectList.Add(obj); }); end = DateTime.Now.Subtract(start).TotalSeconds; Console.Write("Using Parallel...Finish all in "+ end +" second"); Console.ReadLine(); } private static object getData(Person item) { Thread.Sleep(1000); return "Test Object " + item.Id; } } public class Person { public int Id { get; set; } public string Name { get; set; } } 

Output

enter image description here

+1
source

All Articles