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;
}
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/datacool_2017_webapi.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/

浙公网安备 33010602011771号