Using .NET 3.5 or 4? And what do your async fetch / save APIs look like? Are they BeginFetch() / EndFetch() and BeginSave() / EndSave()? If you're using .NET 4, you may want to take a look at the Task Parallel Library (System.Threading.Tasks namespace).
Marked as answer by ricka6 on Mar 09, 2010 08:47 PM
FirstOff: I'm using the same user action to do both fetch,massage and save.
So, in AsncController parlance, I'm using CalculateAsync()/CalculateCompleted().
No, I'm using .NET3.5. The problem, though, is not how to make the service calls in either group to be parallel. My problem is how to wait for each parallel group to finish before I start the next parallel group.
I can see where that only addresses half your question, for the other half of the question:
Pita.O
d. async save all 3 results to data service in parallel.
e.synchronize.
Couldn't you simply call "AsyncManager.OutstandingOperations.Increment(3);" again (at the end). And then perform the "AsyncManager.OutstandingOperations.Decrement();" in each save operation?
Please let me explain further (refering to the original question):
Item (a) is a set of multiple operations which occur asynchroneously in parallel.
Item (b) is a synchronization point similar to a waitstate that ensures that all operations in (a) complete before item (c) starts (similar to what the AutoResetEvent goes in regular thread syncronization.
My question (only question) is: "How does AsynController implementation support for item (c)?"
My question (only question) is: "How does AsynController implementation support for item (c)?"
It doesn't - you need to do that. In your MethodCompleted action, you are synchronized for the pending/parallel operations. I suggest you modify the supplied project and experiment with it.
Using an Asynchronous Controller in ASP.NET MVC will get minor updates soon, but you should consider re-reading it. I'll let the team know you are interested in multiple synch
points.
After you reread my article - reread Levi's advice - If you're using .NET 4, you may want to take a look at the Task Parallel Library (System.Threading.Tasks namespace).
- Call AsyncManager.OutstandingOperations.Increment() once at the very beginning of the request. Don't touch OutstandingOperations again until the very last step below.
- Kick off your three parallel operations. Keep your own separate counter (initialized to 3). As each operation completes, decrement this counter by one.
- When your internal counter hits zero, you know that the first set has completed. Kick off your next set (with a separate counter, initialized to the number of items in the new set). As each operation completes, decrement this counter by one.
- Repeat as necessary for each set.
- When all sets have completed, call AsyncManager.OutstandingOperations.Decrement() to complete the work.
The reason this works is because from the AsyncManager's perspective, your entire block of work is one gigantic asynchronous operation (hence why the counter was incremented / decremented only once). You're going to be kicking off extra work as part of
this single block, but AsyncManager doesn't know of or care about that.
Pita.O
Member
41 Points
22 Posts
How to implement multiple synchronization points on Controller async action.
Feb 28, 2010 11:09 PM|LINK
Hi,
What best synchronization feature should I use in my asynccontroller action? [calling while(OutstandingActions > 0) is not an option)], please.
a. Async fetch 3 items from data service in parallel,
b. synchronize.
c. compute a value using the 3.
d. async save all 3 results to data service in parallel.
e.synchronize.
f. finish
synchronization
levib
Star
7702 Points
1099 Posts
Microsoft
Re: How to implement multiple synchronization points on Controller async action.
Mar 01, 2010 08:09 AM|LINK
Using .NET 3.5 or 4? And what do your async fetch / save APIs look like? Are they BeginFetch() / EndFetch() and BeginSave() / EndSave()? If you're using .NET 4, you may want to take a look at the Task Parallel Library (System.Threading.Tasks namespace).
Pita.O
Member
41 Points
22 Posts
Re: How to implement multiple synchronization points on Controller async action.
Mar 01, 2010 10:49 PM|LINK
FirstOff: I'm using the same user action to do both fetch,massage and save.
So, in AsncController parlance, I'm using CalculateAsync()/CalculateCompleted().
No, I'm using .NET3.5. The problem, though, is not how to make the service calls in either group to be parallel. My problem is how to wait for each parallel group to finish before I start the next parallel group.
Hong-Gang Ch...
All-Star
74696 Points
6768 Posts
Re: How to implement multiple synchronization points on Controller async action.
Mar 05, 2010 07:24 AM|LINK
Please check this article, the article will show you how to solve this issue,
http://msdn.microsoft.com/en-us/library/ee728598(VS.100).aspx
If you have any feedback about my replies,please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
Pita.O
Member
41 Points
22 Posts
Re: How to implement multiple synchronization points on Controller async action.
Mar 06, 2010 02:59 AM|LINK
Hi Hong-Gang,
The articule you referred me to only discussed multiple operations not multiple synchronizations of groups of operations.
Nick Riggs
Member
502 Points
81 Posts
Re: How to implement multiple synchronization points on Controller async action.
Mar 06, 2010 03:25 AM|LINK
I guess that's true, but in that article it had the follow example:
public void IndexAsync(string city) { AsyncManager.OutstandingOperations.Increment(3); NewsService newsService = new NewsService(); newsService.GetHeadlinesCompleted += (sender, e) => { AsyncManager.Parameters["headlines"] = e.Value; AsyncManager.OutstandingOperations.Decrement(); }; newsService.GetHeadlinesAsync(); SportsService sportsService = new SportsService(); sportsService.GetScoresCompleted += (sender, e) => { AsyncManager.Parameters["scores"] = e.Value; AsyncManager.OutstandingOperations.Decrement(); }; sportsService.GetScoresAsync(); WeatherService weatherService = new WeatherService(); weatherService.GetForecastCompleted += (sender, e) => { AsyncManager.Parameters["forecast"] = e.Value; AsyncManager.OutstandingOperations.Decrement(); }; weatherService.GetForecastAsync(); }I can see where that only addresses half your question, for the other half of the question:
Couldn't you simply call "AsyncManager.OutstandingOperations.Increment(3);" again (at the end). And then perform the "AsyncManager.OutstandingOperations.Decrement();" in each save operation?
Pita.O
Member
41 Points
22 Posts
Re: How to implement multiple synchronization points on Controller async action.
Mar 07, 2010 12:32 AM|LINK
Please let me explain further (refering to the original question):
Item (a) is a set of multiple operations which occur asynchroneously in parallel.
Item (b) is a synchronization point similar to a waitstate that ensures that all operations in (a) complete before item (c) starts (similar to what the AutoResetEvent goes in regular thread syncronization.
My question (only question) is: "How does AsynController implementation support for item (c)?"
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: How to implement multiple synchronization points on Controller async action.
Mar 09, 2010 08:57 PM|LINK
It doesn't - you need to do that. In your MethodCompleted action, you are synchronized for the pending/parallel operations. I suggest you modify the supplied project and experiment with it. Using an Asynchronous Controller in ASP.NET MVC will get minor updates soon, but you should consider re-reading it. I'll let the team know you are interested in multiple synch points.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: How to implement multiple synchronization points on Controller async action.
Mar 09, 2010 10:01 PM|LINK
After you reread my article - reread Levi's advice - If you're using .NET 4, you may want to take a look at the Task Parallel Library (System.Threading.Tasks namespace).
levib
Star
7702 Points
1099 Posts
Microsoft
Re: How to implement multiple synchronization points on Controller async action.
Mar 09, 2010 10:14 PM|LINK
You can do this using manual counters. In short:
- Call AsyncManager.OutstandingOperations.Increment() once at the very beginning of the request. Don't touch OutstandingOperations again until the very last step below.
- Kick off your three parallel operations. Keep your own separate counter (initialized to 3). As each operation completes, decrement this counter by one.
- When your internal counter hits zero, you know that the first set has completed. Kick off your next set (with a separate counter, initialized to the number of items in the new set). As each operation completes, decrement this counter by one.
- Repeat as necessary for each set.
- When all sets have completed, call AsyncManager.OutstandingOperations.Decrement() to complete the work.
The reason this works is because from the AsyncManager's perspective, your entire block of work is one gigantic asynchronous operation (hence why the counter was incremented / decremented only once). You're going to be kicking off extra work as part of this single block, but AsyncManager doesn't know of or care about that.