1. 程式人生 > >呼叫C#函式Timeout研究

呼叫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