【问题标题】:MVC4 Ajax wait before executing the next processMVC4 Ajax 在执行下一个进程之前等待
【发布时间】:2013-05-21 08:45:49
【问题描述】:

在我的 MVC 项目中,我生成一个图像数组并将该数组存储为会话变量,我使用滑动条为图像设置动画,并通过计算第一次单击和 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;
    }

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

从我使用 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() {
          }
       });

我有两个问题,首先更新视图需要时间并跳过一些图像,其次是它打开了许多线程并且如果许多用户访问同一页面它会变慢。我想过使用异步,但我仍在使用 c# 4,这可能需要对我的代码进行大量更改。我正在阅读有关 SignalR 的信息,我的问题是可以这样做(假设我只是更新用户屏幕而不是所有用户)还是有更好的解决方案。

我想要实现的事件顺序是:

  1. Ajax 向第一个操作发送请求或生成第一个图像并等待
  2. 图像生成后,Ajax 接收成功,然后使用第二个动作将图像显示在屏幕上
  3. 然后第一个动作生成第二个图像

我看到的挑战是第一个图像不断生成图像而不等待,所以我的问题是我如何让第一个动作等待,以及如何向它发送消息以生成以下图像。

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

【问题讨论】:

  • SignalR 是一对多消息的绝佳解决方案,与 1-1 消息相比,您需要订阅/发布/广播。更新到 .NET 4.5 以使用 async/await 应该没有问题。否则,您始终可以将 TPL(任务并行库)与 .NET 4 框架中的 Task.Factory.StartNew 方法一起使用。
  • 谢谢,可惜VS2010不支持4.5。
  • 没关系,你试过TPL吗?
  • 我现在正在学习,谢谢
  • 我发现学习曲线超出了我的技术专长,我只能完成简单的任务。感谢您提供有关 TPL 的信息。

标签: ajax asp.net-mvc async-await


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用 TPL,你可以试试这个(使用上面的代码),同样可以应用于 animate 方法:

            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 的简写

    【讨论】:

    • 谢谢ianaldo21,因此将Task.Run() 添加到这两个操作中将解决问题!只是为了确定顺序:我使用 ajax 从查看器发送命令到动画方法,完成后,它将图像文件发送到 ImageOut 方法,使用 ajax 成功显示输出图像,然后第二个图像将进行处理。
    • 我收到以下错误:“System.Threading.Tasks.Task”不包含“运行”的定义
    猜你喜欢
    • 2012-12-23
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    相关资源
    最近更新 更多