zoukankan      html  css  js  c++  java
  • 具有timeout 功能的函数调用

    做项目的时候有时经常会需要一个带有timeout功能的函数调用。

    比如从后台读数据并期望在给定时间内返回。借此机会包装了一个简单的C# class, 直接上代码吧.

    public class TimeoutInvokerWrapper
            {
                private ManualResetEvent mTimeoutObject;
                private bool mBoTimeout;
                private Func<object, object> doHandler;
    
                public string Error { get; private set; }
                public object Result { get; private set; }
    
                public TimeoutInvokerWrapper(Func<object, object> handler)
                {
                    this.doHandler = handler;
                    this.mTimeoutObject = new ManualResetEvent(true);
                }
    
                public bool DoWithTimeout(object input, TimeSpan timeSpan)
                {
                    if (this.doHandler == null)
                    {
                        return false;
                    }
    
                    this.mTimeoutObject.Reset();
                    this.mBoTimeout = true;
    
                    this.doHandler.BeginInvoke(input, DoAsyncCallBack, null);
                    if (!this.mTimeoutObject.WaitOne(timeSpan, false))
                    {
                        this.mBoTimeout = true;
                    }
    
                    return this.mBoTimeout;
                }
    
                private void DoAsyncCallBack(IAsyncResult result)
                {
                    try
                    {
                        this.Result = this.doHandler.EndInvoke(result);
                        this.mBoTimeout = false;
                    }
                    catch (Exception ex)
                    {
                        this.mBoTimeout = true;
                        this.Error = ex.ToString();
                    }
                    finally
                    {
                        this.mTimeoutObject.Set();
                    }
                }
            }

    该类可以这样使用

    public static void Main(string[] args)
            {
                Func<object, object> func = (obj) => { return obj.ToString(); };
    
                TimeoutInvokerWrapper wrapper = new TimeoutInvokerWrapper(func);
                bool isTimeout = wrapper.DoWithTimeout(123, TimeSpan.FromSeconds(2));
                if (isTimeout)
                {
                    System.Console.WriteLine("function call timeout.");
                }
                else if (!string.IsNullOrEmpty(wrapper.Error))
                {
                    System.Console.WriteLine("function call does not timeout but throw exception.");
                }
                else
                {
                    System.Console.WriteLine("call succeed.")
                    System.Console.WriteLine(wrapper.Result);   
                }
            }
  • 相关阅读:
    工业级DTU无线数据传输终端
    4G DTU主要应用的场景
    4G DTU在油田远程监控中的应用
    模拟量采集模块哪个品牌好
    模拟量采集是什么?模拟量采集怎么应用?
    串行通信和串口通信有什么区别
    什么是模拟量,模拟量输出,模拟量输入
    嵌入式串口转以太网模块作用
    串口服务器和Modbus网关有什么不同
    SVN客户端的安装配置与使用
  • 原文地址:https://www.cnblogs.com/VectorZhang/p/5365577.html
Copyright © 2011-2022 走看看