关于为什么使用异步Controller,这里不做备忘,三岁小孩都懂。主要的备忘是如何使用AsyncController。

//这个action以Async结尾,并且返回值是void
        public void TestAsync()
        {
            //实现异步action加计数1
            AsyncManager.OutstandingOperations.Increment();
            FileStream fileStream = new FileStream(@"C:\Users\wuxq\Documents\Visual Studio 2010\Projects\MvcAppDemo2\AsyncControllerDemo\并发控制.txt", FileMode.Open);
            byte[] byteArray = new byte[fileStream.Length];
            fileStream.BeginRead(byteArray, 0, (int)fileStream.Length, (IAsyncResult result) =>
            {
                string content = Encoding.Default.GetString(byteArray);
                //参数要放在这个字典里面实现向Completed action传递
                AsyncManager.Parameters["content"] = content;
                //异步action回调结束
                AsyncManager.OutstandingOperations.Decrement();
                fileStream.Close();
            }, null);
        }

        //这个action以Completed为后缀异步action结束后调用的函数,返回值为ActionResult
        public ActionResult TestCompleted(string content)
        {
            Response.Write(content);
            return View();
        }
View Code

相关文章:

  • 2021-06-06
  • 2021-04-20
  • 2022-03-07
  • 2021-08-07
  • 2022-12-23
  • 2022-02-09
  • 2021-12-18
猜你喜欢
  • 2022-01-27
  • 2022-12-23
  • 2021-04-12
  • 2022-02-17
  • 2022-02-21
  • 2021-07-29
相关资源
相似解决方案