예제 #1
0
        private void GatherThreadData(IThreadDataSource threadDataSource)
        {
            foreach (IThread thread in threadDataSource.Threads)
            {
                Process process = new Process(thread.Process.Id, thread.Process.ImageName);

                if (!Threads.ContainsKey(process))
                {
                    Threads.Add(process, new List <Thread>());
                }

                Threads[process].Add(new Thread(thread.Id));
            }
        }
        /// <summary>
        /// 获取默认工作单元
        /// </summary>
        /// <returns>获取当前工作单元,如果不存在就创建一个新的返回</returns>
        private static IUnitOfWork Get()
        {
            if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
            //如果当前线程没有名称,就没有工作单元,设置名称
            {
                Thread.CurrentThread.Name = Guid.NewGuid().ToString();
                return(null);
            }

            lock (Threads.SyncRoot) //根据工作线程获取工作单元
            {
                if (Thread.CurrentThread.Name != null && Threads.ContainsKey(Thread.CurrentThread.Name))
                {
                    return((IUnitOfWork)Threads[Thread.CurrentThread.Name]);
                }
            }

            return(null);
        }