I have a class which does some async work and processes the result. While waiting on task results, the class hands control back to the main thread ("Index").
What is the best practice for passing the processed results to a strongly typed View when complete?
Havent played with async controllers much but I did see some sample code once. As I recall as you said the main action thread will wait for tasks to complete before returning so there should be a way to provide a callback. Google turned this up in 5 seconds:
public class PortalController : AsyncController {
public void NewsAsync(string city) {
AsyncManager.OutstandingOperations.Increment();
NewsService newsService = new NewsService();
newsService.GetHeadlinesCompleted += (sender, e) =>
{
AsyncManager.Parameters["headlines"] = e.Value;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetHeadlinesAsync(city);
}
public ActionResult NewsCompleted(string[] headlines) {
return View("News", new ViewStringModel
{
NewsHeadlines = headlines
});
}
}
So you see here the NewsCompleted is fired only when the async operations have been completed. Whatever is pumped into that Parameters collections seems to get passed to the NewsCompleted action as a method argument.
Unfortunately this example doesn't show where e.Value comes from. It may just be you need to follow the IAsyncResult pattern, I can't find any examples that explain what, for example the NewsService implementation might look like.
-- Sam Critchley
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
From my home controller I instantiate a class named ResolveHost.
ResolveHost _resolve= new ResolveHost();
_resolve.ReadIcmpEcho(_lstPingViewModel);
In the ResolveHost class I process icmpecho request with methods such as Task.Factory.ContinueWhenAll() and WaitAll(). After the ForEach loop completes, I need to display the results taking into consideration that control has been handed back to the main
thread e.g the Index View.
dvLes
Member
83 Points
30 Posts
Redirect to a View from another class method
Mar 22, 2012 11:09 AM|LINK
Morning All,
I have a class which does some async work and processes the result. While waiting on task results, the class hands control back to the main thread ("Index").
What is the best practice for passing the processed results to a strongly typed View when complete?
Les
raduenuca
All-Star
24675 Points
4250 Posts
Re: Redirect to a View from another class method
Mar 22, 2012 11:40 AM|LINK
Can you please show some code because the description is not so "descriptive" ?
Radu Enuca | Blog
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: Redirect to a View from another class method
Mar 22, 2012 11:45 AM|LINK
Havent played with async controllers much but I did see some sample code once. As I recall as you said the main action thread will wait for tasks to complete before returning so there should be a way to provide a callback. Google turned this up in 5 seconds:
public class PortalController : AsyncController { public void NewsAsync(string city) { AsyncManager.OutstandingOperations.Increment(); NewsService newsService = new NewsService(); newsService.GetHeadlinesCompleted += (sender, e) => { AsyncManager.Parameters["headlines"] = e.Value; AsyncManager.OutstandingOperations.Decrement(); }; newsService.GetHeadlinesAsync(city); } public ActionResult NewsCompleted(string[] headlines) { return View("News", new ViewStringModel { NewsHeadlines = headlines }); } }So you see here the NewsCompleted is fired only when the async operations have been completed. Whatever is pumped into that Parameters collections seems to get passed to the NewsCompleted action as a method argument.
Unfortunately this example doesn't show where e.Value comes from. It may just be you need to follow the IAsyncResult pattern, I can't find any examples that explain what, for example the NewsService implementation might look like.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
dvLes
Member
83 Points
30 Posts
Re: Redirect to a View from another class method
Mar 22, 2012 11:51 AM|LINK
Hi Raduenuca, thanks for the reply.
From my home controller I instantiate a class named ResolveHost.
ResolveHost _resolve= new ResolveHost();
_resolve.ReadIcmpEcho(_lstPingViewModel);
In the ResolveHost class I process icmpecho request with methods such as Task.Factory.ContinueWhenAll() and WaitAll(). After the ForEach loop completes, I need to display the results taking into consideration that control has been handed back to the main thread e.g the Index View.
try { foreach (Task<PingReply> echoResult in icmpEchoResult) { int listItem = lstIcmpEcho.FindIndex(p => p.Host == item.Host); lstIcmpEcho[listItem].PingAddress = echoResult.Result.Address.ToString(); lstIcmpEcho[listItem].PingStatus = echoResult.Result.Status.ToString(); } } catch{...}Hope this makes more sense.
Les
dvLes
Member
83 Points
30 Posts
Re: Redirect to a View from another class method
Mar 22, 2012 11:55 AM|LINK
Thanks very much for the reply. Will check it out and revert.