码迷,mamicode.com
首页 > Windows程序 > 详细

webapi 异步返回

时间:2017-08-30 09:52:14      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:bsp   ini   class   dmi   ext   back   技术分享   log   begin   

两年前我遇到一个难题:

https://q.cnblogs.com/q/78177

WebAPI中使用socket如果在server端回复了再返回值?

现在终于做出一种实现了:

 [HttpGet]
        public ApiActionResult OnceBackDemo()
        {
            var result = new ApiActionResult()
            {
                Success = false,
                Result = null,
                Message = "操作失败。"
            };
            System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
            mre.Reset();
            Random rm = new Random();
            Action<Stopwatch> task = (sw) =>
            {
                sw.Start();
                int rm_val = rm.Next(1000);
                System.Threading.Thread.Sleep(rm_val);
            };
            var sw_out = new Stopwatch();
            task.BeginInvoke(sw_out, (ar) =>
            {
                task.EndInvoke(ar);
                mre.Set();
                sw_out.Stop();
                result.Success = true;
                result.Message = DateTime.Now + "->操作成功,耗时:" + sw_out.ElapsedMilliseconds.ToString()+"毫秒。";
            }, null);
            if (!mre.WaitOne(1500))
            {
                return result; 
            }
            return result;
        }

技术分享  

       

webapi 异步返回

标签:bsp   ini   class   dmi   ext   back   技术分享   log   begin   

原文地址:http://www.cnblogs.com/datacool/p/datacool_2017_webapi.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!