zoukankan      html  css  js  c++  java
  • 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;
            }
    

      

           

  • 相关阅读:
    LayoutInflater(布局服务)
    FOTA升级
    APK安装过程及原理详解
    Context类型
    Android应用的persistent属性
    Notification(状态栏通知)详解
    Handler消息传递机制浅析
    Selenium HTMLTestRunner 无法生成测试报告的总结
    【python】远程使用rsa登录sftp,上传下载文件
    02.性能测试中的指标
  • 原文地址:https://www.cnblogs.com/datacool/p/datacool_2017_webapi.html
Copyright © 2011-2022 走看看