c# - 线程完成后如何从ConcurrentDictionary <int,Thread>中删除线程

标签 c# multithreading wcf windows-services

这是我的项目要求:

  • 承载WCF服务的Windows服务。
  • WCF服务必须启动并跟踪许多线程。
  • 每个线程都必须通过提供的 key 来标识。
  • 将有许多不同类型的任务(线程),每个任务都有“一次最多运行”的限制,队列中有其他任务,直到其他相同类型的任务完成为止。

  • 到目前为止,我有:
  • 承载WCF服务的Windows服务(类似于http://msdn.microsoft.com/en-us/library/ms733069(v=vs.110).aspx)。
  • 包含线程的抽象类(使用合成,因为我无法扩展线程)
  • 在WCF服务类中,我有一个名为MyThreadPool的类的静态实例,该类包含一个ConcurrentDictionary,用于记录我正在运行的线程。

  • 我的问题是:
  • 从线程列表中删除已完成的线程的最佳方法是什么(在线程完成时)?
  • 在这种情况下,ConcurrentDictionary的静态实例是管理线程的好方法吗?如果没有,可以建议什么?

  • 我的一些代码如下所示:
    [ServiceContract(Namespace = "...")]
    public interface IMyService    
    {
        [OperationContract]
        void StartProcess(int MIndexId, int MProcessId);
        [OperationContract]
        void StopProcess(int MIndexProcessId);
    }
    
    public class MyService : IMyService
    {
        private static MyThreadPool threadPool = new MyThreadPool();
    
        public void StopProcess(int MIndexProcessId)
        {
            throw new NotImplementedException();
        }
    
        public void StartProcess(int ItemIdToProcess, int ProcessTypeId)
        {
            // call threadPool.LaunchThread(...)
        }
    }
    
    public class MyThreadPool
    {
        private ConcurrentDictionary<int, BaseThread> _threads;
        ...
        public void LaunchThread(BaseThread thread, int ItemIdToProcess)
        {
            // set additional data for thread (such as a key and name) for tracking in a database
            _threads.AddOrUpdate(ItemIdToProcess, thread, (key, oldValue) => { return      oldValue; });
            thread.Start();
        }
        public void KillThread(int ItemIdToProcess)
        { 
            ...
        }
    }
    
    public abstract class BaseThread
    {
        // some additional properties for tracking thread
        // ...
        private Thread _thread;
    
        protected BaseThread()
        { 
            _thread = new Thread(new ThreadStart(this.RunThread));
            _thread.IsBackground = true;
        }
        // Thread methods / properties
        public void Start() { _thread.Start(); }
        public void Join() { _thread.Join(); }
        public bool IsAlive { get { return _thread.IsAlive; } }
        public string Name
        {
            get
            {
                return _thread.Name;
            }
            set
            {
                _thread.Name = value;
            }
        }
        public void Abort()
        {
            _thread.Abort();
        }
        public abstract void RunThread();
    }
    
    public class ValidateThread : BaseThread
    {
        public override void RunThread()
        {
            ...
            // indicate to calling thread to remove from thread list();
        }
    }
    

    最佳答案

  • 完成所有有意义的工作后,使线程自行删除。更好的是,将TaskLongRunning选项一起使用。编写任务很容易。
  • 似乎合理。您已经避免了很多陷阱,并且设计看起来很可靠。 IIS中托管的WCF服务的一个陷阱是工作进程可以随时死亡。您可以使用Windows服务来避免这种情况。

  • Windows服务像其他任何其他应用程序一样,大多是由外部启动的exe。

    但是,一件不好的事情是:_thread.Abort(); Thread.Abort is evil。 .NET中的取消必须(不应该)合作。

    关于c# - 线程完成后如何从ConcurrentDictionary <int,Thread>中删除线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22788578/

    相关文章:

    C# 数据库插入 (ASP.NET) - ExecuteNonQuery : CommandText property has not been initialized

    c# - 使用 Epplus 的 Excel 时间格式

    c++ - 使用 mutex 和 condition_variable 时的异常

    c# - 如何在 asp.net mvc 3 razor View 中访问应用程序变量?

    c# - 比较两个复杂对象的相等性

    java 矩阵乘法 (FAST)

    Java 线程组织指南

    c# - 未触发 WCF 客户端关闭事件

    wcf - netTcpBinding 最快的安全配置是什么?

    c# - 从 WCF 服务调用 Windows 服务中的方法(自托管)