C#效率极子 - 精益求精
自动化+性能优化

有时候我们需要给线程传参,而.net的ParameterizedThreadStart不支持泛型,参数只能是object,使用起来有点小不爽的感觉。其实只要简单的包装一下Thread,就能在使用的时候心情舒畅了。

    /// <summary>
    /// 泛型参数线程
    /// </summary>
    /// <typeparam name="parameterType">参数类型</typeparam>
    public struct thread<parameterType>
    {
        /// <summary>
        /// 线程委托
        /// </summary>
        private Action<parameterType> Handle;
        /// <summary>
        /// 线程参数
        /// </summary>
        private parameterType Parameter;
        /// <summary>
        /// 初始化 泛型参数线程
        /// </summary>
        /// <param name="handle">线程委托</param>
        /// <param name="parameter">线程参数</param>
        public thread(Action<parameterType> handle, parameterType parameter)
        {
            if (handle == null) new showjim.sys.debug("缺少 线程委托").throwException();
            Handle = handle;
            Parameter = parameter;
        }
        /// <summary>
        /// 启动线程并返回线程句柄
        /// </summary>
        /// <returns>线程句柄</returns>
        public Thread start()
        {
            Thread thread = new Thread(ParameterThread);
            thread.IsBackground = true;
            thread.Start();
            return thread;
        }
        /// <summary>
        /// 调用线程委托
        /// </summary>
        private void ParameterThread()
        {
            Handle(Parameter);
        }
        /// <summary>
        /// 启动线程并返回线程句柄
        /// </summary>
        /// <param name="handle">线程委托</param>
        /// <param name="parameter">线程参数</param>
        /// <returns>线程句柄</returns>
        public static Thread start(Action<parameterType> handle, parameterType parameter)
        {
            return new thread<parameterType>(handle, parameter).start();
        }
    }

对于有返回值的线程调用,是否需要回调功能呢,索性一起搞定吧。

    /// <summary>
    /// 带返回值的泛型参数线程
    /// </summary>
    /// <typeparam name="parameterType">参数类型</typeparam>
    /// <typeparam name="returnType">返回值类型</typeparam>
    public struct thread<parameterType, returnType>
    {
        /// <summary>
        /// 线程委托
        /// </summary>
        private Func<parameterType, returnType> Handle;
        /// <summary>
        /// 线程参数
        /// </summary>
        private parameterType Parameter;
        /// <summary>
        /// 返回值处理委托
        /// </summary>
        private Action<returnType> OnReturn;
        /// <summary>
        /// 初始化 带返回值的泛型参数线程
        /// </summary>
        /// <param name="handle">线程委托</param>
        /// <param name="parameter">线程参数</param>
        /// <param name="onReturn">返回值处理委托</param>
        public thread(Func<parameterType, returnType> handle, parameterType parameter, Action<returnType> onReturn)
        {
            if (handle == null) new showjim.sys.debug("缺少 线程委托").throwException();
            Handle = handle;
            Parameter = parameter;
            OnReturn = onReturn;
        }
        /// <summary>
        /// 启动线程并返回线程句柄
        /// </summary>
        /// <returns>线程句柄</returns>
        public Thread start()
        {
            Thread thread = new Thread(ParameterThread);
            thread.IsBackground = true;
            thread.Start();
            return thread;
        }
        /// <summary>
        /// 调用线程委托
        /// </summary>
        private void ParameterThread()
        {
            returnType returnValue = default(returnType);
            try
            {
                returnValue = Handle(Parameter);
            }
            finally
            {
                if (OnReturn != null) OnReturn(returnValue);
            }
        }
        /// <summary>
        /// 启动线程并返回线程句柄
        /// </summary>
        /// <param name="handle">线程委托</param>
        /// <param name="parameter">线程参数</param>
        /// <param name="onReturn">返回值处理委托</param>
        /// <returns>线程句柄</returns>
        public static Thread start
            (Func<parameterType, returnType> handle, parameterType parameter, Action<returnType> onReturn)
        {
            return new thread<parameterType, returnType>(handle, parameter, onReturn).start();
        }
    }
posted on 2012-05-14 14:40  肖进  阅读(835)  评论(0编辑  收藏  举报