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);   
                }
            }
  • 相关阅读:
    linux:centOs7换源阿里云
    nginx:负载均衡实战(一)
    linux-安装jdk以及tomcat
    nginx:在linux上进行nginx的安装
    开源 免费 java CMS
    使用PHP获取汉字的拼音(全部与首字母)
    php版获取重定向后地址的代码分享
    php获取数组中重复数据的两种方法
    php删除html标签的三种解决方法
    php curl 伪造IP来源的代码分享
  • 原文地址:https://www.cnblogs.com/VectorZhang/p/5365577.html
Copyright © 2011-2022 走看看