views:

418

answers:

1

Currently MS has been release asp.net MVC 2.0 beta with the AsyncController in it but there are very few document about this except one document at: http://msdn.microsoft.com/en-us/library/ee728598%28VS.100%29.aspx

The example from microsoft show that there is an event was used in the process of making an AsyncController.

However, there are people like me, who do not use any event to work with the Decrement and wanting to migrate a controller from Sync to Async... So is there any way to handle this?

Example, i have:

Image orgImage = _db.getImagebyGUID(guid);
string date = orgImage.CREATE_DATE.toString();
Image newImage = _db.getImagebyGUID(guid2);
string date2 = newImage .CREATE_DATE.toString();

is there any possible way to use the Async without event because there are lot lot of class that do not implement event callback...

Thank you very much

+1  A: 

You could always put this code into a method, create a delegate to this method and invoke it asynchronously:

public string DoWork()
{
    Image orgImage = _db.getImagebyGUID(guid);
    string date = orgImage.CREATE_DATE.toString();
    Image newImage = _db.getImagebyGUID(guid2);
    string date2 = newImage .CREATE_DATE.toString();
    return date + date2;
}

And the invoke code:

AsyncManager.OutstandingOperations.Increment();
Func<string> doWorkHandler = DoWork;
doWorkHandler.BeginInvoke(ar =>
{
    var handler = (Func<string>)ar.AsyncState;
    AsyncManager.Parameters["date"] = handler.EndInvoke(ar);
    AsyncManager.OutstandingOperations.Decrement();
}, doWorkHandler);
Darin Dimitrov
when i trying to add the param to DoWork function, it's not going to work, why is it? i need to pass at least a model to the function since its stick together
DucDigital
Cannot implicitly convert type 'System.Drawing.Image' to System.Func<System.Drawing.Image>'
DucDigital
This is not a good candidate to be made into an asynchronous action because it's still consuming a thread from ASP.NET's thread pool.
Levi