调用C#函数Timeout研究

做了一个函数timeout的简单研究,代码如下:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            CallFunctionWithTimeout(GetResult, 10, 20, 2 * 1000, GetResultCallback, GetResultTimeoutCallback);
        }

        private int GetResult(int arg1, int arg2)
        {
            Thread.Sleep(5 * 1000);
            return arg1 + arg2;
        }

        private void GetResultCallback(int result)
        {
            System.Diagnostics.Trace.WriteLine("Total: " + result);
        }

        private void GetResultTimeoutCallback()
        {
            System.Diagnostics.Trace.WriteLine("The call is timeout.");
        }

        public static void CallFunctionWithTimeout<T>(Action<T> function, T arg, int timeout, Action callback, Action timeoutCallback)
        {
            if (function == null)
            {
                throw new ArgumentNullException("function");
            }
            CallFunctionWithTimeout(new Action(() => function(arg)), timeout, callback, timeoutCallback);
        }

        public static void CallFunctionWithTimeout<T1, T2>(Action<T1, T2> function, T1 arg1, T2 arg2, int timeout, Action callback, Action timeoutCallback)
        {
            if (function == null)
            {
                throw new ArgumentNullException("function");
            }
            CallFunctionWithTimeout(new Action(() => function(arg1, arg2)), timeout, callback, timeoutCallback);
        }

        public static void CallFunctionWithTimeout(Action function, int timeout, Action callback, Action timeoutCallback)
        {
            if (function == null)
            {
                throw new ArgumentNullException("function");
            }
            Action action = new Action(() =>
            {
                IAsyncResult ar = function.BeginInvoke(null, null);
                if (ar.AsyncWaitHandle.WaitOne(timeout, false))
                {
                    if (callback != null)
                    {
                        callback();
                    }
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Call function {0} is timeout.", function.Method.Name));
                    if (timeoutCallback != null)
                    {
                        timeoutCallback();
                    }
                }
            });

            action.BeginInvoke(null, null);
        }

        public static void CallFunctionWithTimeout<T, TResult>(Func<T, TResult> function, T arg, int timeout, Action<TResult> callback, Action timeoutCallback)
        {
            if (function == null)
            {
                throw new ArgumentNullException("function");
            }
            CallFunctionWithTimeout(new Func<TResult>(() => { return function(arg); }), timeout, callback, timeoutCallback);
        }

        public static void CallFunctionWithTimeout<T1, T2, TResult>(Func<T1, T2, TResult> function, T1 arg1, T2 arg2, int timeout, Action<TResult> callback, Action timeoutCallback)
        {
            if (function == null)
            {
                throw new ArgumentNullException("function");
            }
            CallFunctionWithTimeout(new Func<TResult>(() => {return function(arg1, arg2);}), timeout, callback, timeoutCallback);
        }

        public static void CallFunctionWithTimeout<TResult>(Func<TResult> function, int timeout, Action<TResult> callback, Action timeoutCallback)
        {
            if (function == null)
            {
                throw new ArgumentNullException("function");
            }

            Action action = new Action(() =>
            {
                IAsyncResult ar = function.BeginInvoke(null, null);
                if (ar.AsyncWaitHandle.WaitOne(timeout, false))
                {
                    if (callback != null)
                    {
                        callback(function.EndInvoke(ar));
                    }
                }
                else
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Call function {0} is timeout.", function.Method.Name));
                    if (timeoutCallback != null)
                    {
                        timeoutCallback();
                    }
                }
            });

            action.BeginInvoke(null, null);
        }
希望对大家有所帮助,也希望大家拍砖。

摘自:http://www.cnblogs.com/havefun/archive/2012/08/21/2649540.html

posted @ 2016-07-12 23:31  Leytton  阅读(748)  评论(0编辑  收藏  举报