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);   
                }
            }
  • 相关阅读:
    观察者模式
    饿汉单例模式 and 懒汉单例模式
    解决hash冲突之分离链接法
    bat处理文件
    使用json-org包实现POJO和json的转换
    并发修改异常(ConcurrentModificationException)
    封装特效记录--持续更新
    vue loading组件
    vue授权页面登陆之后返回之前的页面
    vue 路由权限
  • 原文地址:https://www.cnblogs.com/VectorZhang/p/5365577.html
Copyright © 2011-2022 走看看