MVC4 Ajax在执行下一个进程之前等待

MVC4 Ajax在执行下一个进程之前等待,ajax,asp.net-mvc,async-await,Ajax,Asp.net Mvc,Async Await,在我的MVC项目中,我生成了一个图像数组,并将该数组存储为会话变量。我使用slidebar为图像设置动画,并通过计算鼠标在画布上移动时第一次单击与x位置之间的距离,在鼠标按下时检测鼠标移动 在我使用的控制器中: public ActionResult Animate(int slice = 0, int udm = 0) { FileContentResult data; Image objImage = null; Bitmap im =

在我的MVC项目中,我生成了一个图像数组,并将该数组存储为会话变量。我使用slidebar为图像设置动画,并通过计算鼠标在画布上移动时第一次单击与x位置之间的距离,在鼠标按下时检测鼠标移动

在我使用的控制器中:

public ActionResult Animate(int slice = 0, int udm = 0)
    {
        FileContentResult data;
        Image objImage = null;
        Bitmap im = null;
        try
        {
                im = MySession.Current.imageArray[slice];
                ....
               MySession.Current.image = im;
            }
            else
            {
                return RedirectToAction("Index",new {.... });
            }
        }
        catch { }
        return null;
    }

从我使用Ajax的角度来看:

  $.ajax({
  url: '/Home/Animate',
  type: 'POST',
  async: false,
  data: {
        slice: ((lastX - firstX) + nSlice),
        udm: ++udm
        },
  success: function(data) {
   if (data.udm) {
   nSlice = (data.slice);
    image.src = '/Home/ImageOut?' + $.param({
     udm: data.udm
     });
     }
       },
        error: function() {
          }
       });
我有两个问题,第一个问题是更新视图需要时间并跳过许多图像,第二个问题是它打开了许多线程,如果有许多用户访问同一页面,那么速度会减慢。我曾想过使用async,但我仍在使用c#4,这可能需要对代码进行大量更改。我在读关于SignalR的文章,我的问题是这可以做到吗(只要我更新用户屏幕而不是所有用户),或者有更好的解决方案吗

我希望实现的事件顺序是:

  • Ajax向第一个操作发送请求或生成第一个映像,然后等待
  • 生成图像时,Ajax接收成功,然后使用第二个操作在屏幕上显示图像
  • 然后,第一个动作生成第二个图像
  • 我看到的挑战是第一个映像不等待就一直生成映像,因此我的问题是如何让第一个操作等待,以及如何向其发送消息以生成下面的映像


    我刚刚安装了VS2012 c#5,有没有什么例子可以帮助我!!非常感谢您的建议,提前谢谢。

    使用TPL,您可以尝试此方法(使用上面的代码),同样可以应用于动画方法:

            public ActionResult ImageOut(int udm = 0)
            {
               FileContentResult data = null;
               Image objImage = null;
               Task.Run(() =>
                   {
                       Bitmap im = MySession.Current.dicomImage;
                       objImage = im.Bitmap(outputSize, PixelFormat.Format24bppRgb, m);
                       using (var memStream = new MemoryStream())
                       {
                           objImage.Save(memStream, ImageFormat.Png);
                           data = this.File(memStream.GetBuffer(), "image/png");
                       }
                   });
               objImage.Dispose();
               return data;
            }
    

    Task.Run只是Task.Factory.StartNew的简写,而不是因为学习曲线而将我的程序更改为使用TPL;我刚刚在ajax中添加了async:false;这有助于延迟屏幕的刷新。这不是最好的方法,但有一点帮助。

    SignalR是一个很好的一对多消息解决方案,订阅/发布/广播与1-1消息相比,似乎是您所需要的。更新到.NET4.5以使用async/await应该没有问题。否则,您可以始终使用TPL(Task Parallel Library)和.NET 4 framework中的Task.Factory.StartNew方法。谢谢,不幸的是VS2010不支持4.5。那很好,您尝试过TPL吗?我现在正在学习,thanksI发现学习曲线取决于我的技术专长,我可以只做简单的任务。感谢您提供有关TPL的信息。谢谢您,因此将Task.Run()添加到这两个操作中将解决此问题!为了确保顺序:我使用ajax从查看器发送命令来设置menthod的动画,完成后,它会将图像文件发送到ImageOut方法,使用ajax成功显示输出图像,然后将处理第二个映像。我收到以下错误:“System.Threading.Tasks.Task”不包含“Run”的定义
            public ActionResult ImageOut(int udm = 0)
            {
               FileContentResult data = null;
               Image objImage = null;
               Task.Run(() =>
                   {
                       Bitmap im = MySession.Current.dicomImage;
                       objImage = im.Bitmap(outputSize, PixelFormat.Format24bppRgb, m);
                       using (var memStream = new MemoryStream())
                       {
                           objImage.Save(memStream, ImageFormat.Png);
                           data = this.File(memStream.GetBuffer(), "image/png");
                       }
                   });
               objImage.Dispose();
               return data;
            }