I'm using Asp.Net 4.0.
I also have a HashSet from which I want to create and run tasks asynchronously, waiting for them all to finish with a timeout applied. The code below is what I have so far, but given that there are so many ways to skin a cat with TPL, can this be improved on, or are there any critical issues?
TaskWrapper[] taskWrappers = new TaskWrapper[nodeCount]; Task<ResponseWrapper>[] tasks = new Task<ResponseWrapper>[nodeCount]; int i = 0; foreach (var n in rt.Nodes) { tasks[i] = Task<ResponseWrapper>.Factory.StartNew(() => MyMethod.Submit(n.Data, timeout)); taskWrappers[i].LeadPointID = n.LeadPointID; // other wrapper stuff; taskWrappers[i].Task = tasks[i]; i++; } Task.WaitAll(tasks, timeout); Having read the article kindly provided, it seems like this kind of code structure would be the preferred method of working:
class MyService2 { public int CalculateMandelbrot() { // Tons of work to do in here! for (int i = 0; i != 10000000; ++i) ; return 42; } } private async void MyButton_Click(object sender, EventArgs e) { await Task.Run(() => myService.CalculateMandelbrot()); } However, as I've previously pointed out I don't have access to VS2012, so given that as a starting point, is there a VS2010 equivalent to this pattern?
Select(e.g.rt.Nodes.Select(n => ...)).