异步控制器asp.net mvc 2.0 [英] Async Controller asp.net mvc 2.0

查看:134
本文介绍了异步控制器asp.net mvc 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,MS已经发布了asp.net MVC 2.0测试版,其中包含AsyncController,但除了以下文档之外,除了以下文档之外,还有很少的文档: http://msdn.microsoft.com/en-us/library/ee728598(VS.100).aspx



微软的示例显示在制作一个AsyncController的过程中使用了一个事件。



然而,那里是像我这样的人,谁不使用任何事件来处理Decrement并希望将控制器从Sync迁移到Async ...所以有什么办法来处理吗?



示例,我有:

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

有没有任何可能的方法来使用Async没有事件,因为有很多类没有实现事件回调...



非常感谢

解决方案

您可以随时将此代码放入一个方法中,为该方法创建一个委托并异步调用:

  public string DoWork 
{
图像orgImage = _db.getImagebyGUID(guid);
string date = orgImage.CREATE_DATE.toString();
Image newImage = _db.getImagebyGUID(guid2);
string date2 = newImage .CREATE_DATE.toString();
返回日期+ date2;
}

而调用代码:

  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);


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(VS.100).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

解决方案

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);

这篇关于异步控制器asp.net mvc 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆