zoukankan      html  css  js  c++  java
  • 线程池,封装使用,实现控制子线程

    类代码
     
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;

    namespace TxWeb.CnForums
    {
        public class TxThread
        {
            public delegate void Delegate(object obj);

            /// <summary>
            
    /// 执行指定的方法,如果在指定的时间之内没有完成,则中止
            
    /// </summary>
            
    /// <param name="func">任务过程</param>
            
    /// <param name="threadID">帖子编号</param>
            
    /// <param name="timeSpan">超时时间</param>
            
    /// <param name="timeoutCallback">如果超时,则调用该方法</param>
            
    /// <param name="updateID">变化主表记录编号</param>
            
    /// <returns>是否正确执行完毕</returns>
            public static bool Call(Delegate func, object threadID, TimeSpan timeSpan, Delegate timeoutCallback, object updateID)
            {
                if (func == null)
                    throw new ArgumentNullException("func");

                ManualResetEvent resetEvent = new ManualResetEvent(false);
                ManualResetEvent waitThreadEvent = new ManualResetEvent(false);

                Exception error = null;
                Thread thread = null;

                // 将任务加到线程当中
                ThreadPool.QueueUserWorkItem(delegate
                {

                    thread = Thread.CurrentThread;
                    try { func(threadID); }
                    catch (ThreadAbortException) { }
                    catch (Exception ex) { error = ex; }

                    resetEvent.Set();
                    // 每次线程执行结束都等待后续的处理逻辑
                    waitThreadEvent.WaitOne();
                });

                try
                {
                    // 等待任务的结束
                    bool result = resetEvent.WaitOne(timeSpan, false);

                    // 说明在执行过程中出现异常,直接抛出异常
                    if (error != null)
                    {
                        throw error;
                    }

                    if (!result)
                    {
                        if (thread != null)
                        {
                            // 此时可以确保该线程没有开始运行新的任务
                            thread.Abort();
                            waitThreadEvent.Set();
                        }

                        if (timeoutCallback != null)
                            timeoutCallback(updateID);
                    }

                    return result;
                }
                finally
                {
                    // 最后确保释放线程池线程
                    waitThreadEvent.Set();
                }
            }
        }
    }
     
    调用:
                    TxWeb.CnForums.TxThread.Delegate createHtml = new TxWeb.CnForums.TxThread.Delegate(CreateHtml);
                    TxWeb.CnForums.TxThread.Delegate dealOverTime = new TxWeb.CnForums.TxThread.Delegate(DealOverTime);
                    TxWeb.CnForums.TxThread.Call(createHtml, threadID, TimeSpan.FromSeconds(OverTime), dealOverTime, updateID);
  • 相关阅读:
    发送邮箱验证信息的工具类
    Tensor的组合与分块-02
    09-ImageJ的安装与使用
    01 织布缺陷——断针缺陷检测
    Map 与结构体的混合使用
    c++ 读取TXT文件,中文乱码处理
    Code128 混合编码--译码方式及校验准则
    08-局部阈值分割算法总结
    code128码国标
    vector使用的相关博客
  • 原文地址:https://www.cnblogs.com/dajiang02/p/2263100.html
Copyright © 2011-2022 走看看