첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 프로세스 카운트
        /// </summary>
        private int processCount;
        
        /// <summary>
        /// 쓰레드 카운트
        /// </summary>
        private int threadCount;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.loadButton.Click += loadButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 로드하기 버튼 클릭시 처리하기 - loadButton_Click(sender, e)

        /// <summary>
        /// 로드하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void loadButton_Click(object sender, EventArgs e)
        {
            this.treeView.Nodes.Clear();

            Dictionary<int, ProcessInformation> dictionary = new Dictionary<int, ProcessInformation>();

            foreach(Process process in Process.GetProcesses())
            {
                dictionary.Add(process.Id, new ProcessInformation(process));
            }

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT ProcessId, ParentProcessId FROM Win32_Process");

            ManagementObjectCollection collection = searcher.Get();

            foreach(ManagementBaseObject item in collection)
            {
                int childID  = Convert.ToInt32(item["ProcessId"      ]);
                int parentID = Convert.ToInt32(item["ParentProcessId"]);

                ProcessInformation childProcessInformation  = null;
                ProcessInformation parentProcessInformation = null;

                if(dictionary.ContainsKey(childID))
                {
                    childProcessInformation = dictionary[childID];
                }

                if(dictionary.ContainsKey(parentID))
                {
                    parentProcessInformation = dictionary[parentID];
                }

                if((childProcessInformation != null) && (parentProcessInformation != null))
                {
                    parentProcessInformation.ChildList.Add(childProcessInformation);

                    childProcessInformation.Parent = parentProcessInformation;
                }
            }

            List<ProcessInformation> list = dictionary.Values.ToList();

            list.Sort();

            processCount = 0;
            threadCount  = 0;

            foreach(ProcessInformation processInformation in list)
            {
                if(processInformation.Parent != null)
                {
                    continue;
                }

                AddTreeViewNode(treeView.Nodes, processInformation);
            }

            this.countLabel.Text = $"프로세스 : {processCount}, 쓰레드 : {threadCount}";
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 트리뷰 노드 추가하기 - AddTreeViewNode(nodeCollection, processInformation)

        /// <summary>
        /// 트리뷰 노드 추가하기
        /// </summary>
        /// <param name="nodeCollection">노드 컬렉션</param>
        /// <param name="processInformation">프로세스 정보</param>
        private void AddTreeViewNode(TreeNodeCollection nodeCollection, ProcessInformation processInformation)
        {
            TreeNode processNode = nodeCollection.Add(processInformation.ToString());

            processNode.Tag = processInformation;

            processCount++;

            if(processInformation.Process.Threads.Count > 0)
            {
                TreeNode threadNode = processNode.Nodes.Add("쓰레드");

                foreach(ProcessThread thread in processInformation.Process.Threads)
                {
                    threadNode.Nodes.Add(string.Format("쓰레드 {0}", thread.Id));

                    threadCount++;
                }
            }

            processInformation.ChildList.Sort();

            foreach(ProcessInformation childProcessInformation in processInformation.ChildList)
            {
                AddTreeViewNode(processNode.Nodes, childProcessInformation);
            }

            if(processInformation.ChildList.Count > 0)
            {
                processNode.Expand();
            }
        }

        #endregion
    }
}
728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요