webapi 异步返回
两年前我遇到一个难题:
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; }
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"作者:" 数据酷软件工作室
"出处:" http://datacool.cnblogs.com
"专注于CMS(综合赋码系统),MES,WCS(智能仓储设备控制系统),WMS,商超,桑拿、餐饮、客房、足浴等行业收银系统的开发,15年+从业经验。因为专业,所以出色。"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"作者:" 数据酷软件工作室
"出处:" http://datacool.cnblogs.com
"专注于CMS(综合赋码系统),MES,WCS(智能仓储设备控制系统),WMS,商超,桑拿、餐饮、客房、足浴等行业收银系统的开发,15年+从业经验。因为专业,所以出色。"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++