- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[C#] Viết ứng dụng Task Manager hiển thị các process đang chạy trên máy tính
Task manager là một công cụ của Windows nhằm để quản lý các ứng dụng đang chạy trên máy tính, giúp chúng ta có thể dễ dàng theo dõi các ứng dụng nào đang chạy, tốn bao nhiêu RAM, CPU, dung lượng Disk...
[C#] Tutorial Task Manager Process in Csharp Winform
Chúng ta, thường sử dụng Task manager để đóng các ứng dụng khi bị treo bằng cách chọn task kill trên process ứng dụng mà chúng ta chọn.
Vì thế, bài viết này mình sẽ hướng dẫn các bạn viết một ứng dụng hiển thị các ứng dụng đang chạy giống TaskManager của windows.
Đầu tiên, các bạn cần tạo một listview vào winform như hình của mình ở bên dưới:
- Các các set View trong Property là: Detail nhé.
- Tiếp tục các bạn tạo 6 column trong listview theo thứ tự: Process name, PID, Status, User name, Memory, Description.
Giao diện demo ứng dụng Task Manager C#

Bước 1: Đầu tiên các bạn cần import thư viện vào
using System.Diagnostics;
using System.Management;
using System.Dynamic;Các bạn, xem hình ảnh của mình ở bên dưới để import ba thư viện ở trên vào nhé.

Bước 2: Chúng ta sẽ lấy danh sách các process theo hàm bên dưới
public void renderProcessesOnListView()
        {           
            Process[] processList = Process.GetProcesses();          
            ImageList Imagelist = new ImageList();
           
            foreach (Process process in processList)            {
               
                string status = (process.Responding == true ? "Responding" : "Not responding");              
                dynamic extraProcessInfo = GetProcessExtraInformation(process.Id);               
                string[] row = {                   
                    process.ProcessName,                    
                    process.Id.ToString(),                   
                    status,                   
                    extraProcessInfo.Username,                 
                    BytesToReadableValue(process.PrivateMemorySize64),                   
                    extraProcessInfo.Description
                };
                try
                {
                    Imagelist.Images.Add(                       
                        process.Id.ToString(),                        
                        Icon.ExtractAssociatedIcon(process.MainModule.FileName).ToBitmap()
                    );
                }
                catch { }
               
                ListViewItem item = new ListViewItem(row)
                {                   
                    ImageIndex = Imagelist.Images.IndexOfKey(process.Id.ToString())
                };
                listView1.BeginInvoke(new Action(() =>
                {
                    listView1.Items.Add(item);
                }));
               
            }
            listView1.BeginInvoke(new Action(() =>
            {                listView1.LargeImageList = Imagelist;
                listView1.SmallImageList = Imagelist;
            }));
           
        }Bước 3: Chúng ta sẽ viết hàm quy đổi các đơn vị của Memory theo B, KB, GB...
public string BytesToReadableValue(long number)
        {
            List suffixes = new List { " B", " KB", " MB", " GB", " TB", " PB" };
            for (int i = 0; i < suffixes.Count; i++)
            {
                long temp = number / (int)Math.Pow(1024, i + 1);
                if (temp == 0)
                {
                    return (number / (int)Math.Pow(1024, i)) + suffixes[i];
                }
            }
            return number.ToString();
        }Bước 4: Tiếp tục viết hàm lấy các thông tin chi tiết của từng process: status, memory, description, username...
Chúng ta sẽ sử dụng lớp Dynamic, để lưu trữ các thông tin của từng process
public ExpandoObject GetProcessExtraInformation(int processId)
        {           
            string query = "Select * From Win32_Process Where ProcessID = " + processId;
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            ManagementObjectCollection processList = searcher.Get();
           
            dynamic response = new ExpandoObject();
            response.Description = "";
            response.Username = "Unknown";
            foreach (ManagementObject obj in processList)
            {
                
                string[] argList = new string[] { string.Empty, string.Empty };
                int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
                if (returnVal == 0)
                {                   
                    response.Username = argList[0];                    
                }
             
                if (obj["ExecutablePath"] != null)
                {
                    try
                    {
                        FileVersionInfo info = FileVersionInfo.GetVersionInfo(obj["ExecutablePath"].ToString());
                        response.Description = info.FileDescription;
                    }
                    catch { }
                }
            }
            return response;
        }Bước 5: Viết hàm khi click vào button thì get danh sách các process đang chạy.
 private void btnGet_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() => {
                renderProcessesOnListView();
            });
            
        }HAVE FUN :)
CẢM ƠN CÁC BẠN ĐÃ THEO DÕI BÀI VIẾT

![[C#] Viết ứng dụng Task Manager hiển thị các process đang chạy trên máy tính](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui](https://laptrinhvb.net/uploads/source/new_image_baiviet/console_gui.png)
![[C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/make_trial_soft.png)
![[C#] Giới thiệu Singleton trong Design Pattern - Duy nhất một thể hiện](https://laptrinhvb.net/uploads/source/image_baiviet/2d5c9c80cf9a3f38647ffeba0a86ac09.png)
![[C#] Hướng dẫn sử dụng delegate để truyền dữ liệu giữa 2 form](https://laptrinhvb.net/uploads/source/image_baiviet/15b1f5447fac48dcf46bc7bb7031272e.png)
![[C#] Viết ứng khóa lock windows lập trinh csharp](https://laptrinhvb.net/uploads/source/image_baiviet/cdfc6c3cc903875de8c90da23f1dbc65.jpg)
![[C#] Xem thông tin đăng ký tên miền Who is Domain](https://laptrinhvb.net/uploads/source/vbnet/whois_domain_thumb.jpg)
![[C#] Tìm kiếm file trùng nhau trong cùng thư mục lập trình Winform](https://laptrinhvb.net/uploads/source/csharp/duplicate_file_thumb.png)
![[C#] Chia sẽ thư viện class làm việc với FTP SERVER bằng Csharp](https://laptrinhvb.net/uploads/source/new_image_baiviet/ftp_class.png)
![[C#] Hướng dẫn đọc file excel đơn giản sử dụng thư viện Epplus](https://laptrinhvb.net/uploads/source/new_image_baiviet/EPPlus.png)
![[C#] Hướng dẫn sử dụng thư viện Quartz lập lịch công việc hàng ngày](https://laptrinhvb.net/uploads/source/image_baiviet/3036de5d746ae1b36cdd04acca07f3d2.png)

![[C#] Lắng nghe sự kiện System Event Windows trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/system_event_csharp.png)
![[C#] Hướng dẫn chuyển đổi chuỗi sang nhị phân và ngược lại](https://laptrinhvb.net/uploads/source/image_baiviet/20c1faac8453ae1604adc8c2daf7d391.jpg)
![[C#] Fake Blue Screen BSOD in winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/fake_bsod.png)
![[C#] Giải pháp thay thế Web Browser Control mặc định bằng Awesomium của bộ Visual Studio](https://laptrinhvb.net/uploads/source/image_baiviet/0ddd54d36f9c30418c8465205ee441d2.png)
![[C#] Hướng dẫn viết ứng dụng tự động thực hiện cuộc gọi fb (Call Video Facebook Messager)](https://laptrinhvb.net/uploads/source/csharp/call_facebook_thumb.jpg)
![[C#] Tìm kiếm xem danh sách từ khóa có tồn tại trong đoạn văn bản hay không](https://laptrinhvb.net/uploads/source/new_image_baiviet/web_client_download_string.png)
![[C#] Hướng dẫn sử dụng thư viện Input Simulator để làm việc với Keyboard, Mouse Virtual](https://laptrinhvb.net/uploads/source/vbnet/Input_Simulator_csharp_thumb.jpg)
![[C#] Sử dụng FolderBrowserDialog Vista trên Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/folder_brower_vista.png)
![[C#] Hướng dẫn custom Button trên Winform](https://laptrinhvb.net/uploads/source/vbnet/custom_button.jpg)
![[C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary](https://laptrinhvb.net/uploads/source/vbnet/search_file_csharp_thumb.png)
![[C#] Hướng dẫn thực thi tập tin python trong winform](https://laptrinhvb.net/uploads/source/vbnet/maxresdefault.jpg)
![[C#] Backup database sqlsever with progress sử dụng thư viện Sql SMO](https://laptrinhvb.net/uploads/source/image_baiviet/cfef35ccbc5d9ec9124e82482451736e.jpg)
![[C#] Hướng dẫn sử dụng Parallel.Invoke() trong lập trình đa luồng Winform](https://laptrinhvb.net/uploads/source/csharp/parallel_invoke_csharp_thumb.png)
![[C#] Hướng dẫn thêm, lưu, xóa, sửa, tìm kiếm trên SQLSERVER CE (SQL COMPACT)](https://laptrinhvb.net/uploads/source/image_baiviet/9695e729194410d52eb4a1e8234e9e0c.jpg)
