- [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#] Giới thiệu và tìm hiểu những tính năng mới của C# 7.0 trong Visual Studio 2017](https://laptrinhvb.net/uploads/source/image_baiviet/00be3133b47e15513e7c8bd46c8030eb.png)
![[C#] Convert Json to Gson class java sử dụng Http Request website jsonschema2pojo](https://laptrinhvb.net/uploads/source/csharp/json_2gson_thumb.png)
![[C#] Tra cứu mã số thuế cá nhân bằng CMND hoặc CCCD](https://laptrinhvb.net/uploads/source/vbnet/tracuu_mst_thumb.jpg)
![[C#] Chia sẻ source code Download dữ liệu máy chấm công Ronald Jack sử dụng thư viện Zkemkeeper.dll](https://laptrinhvb.net/uploads/source/new_image_baiviet/att_log_timekeeper.png)
![[C#] Hướng dẫn tạo mã qrcode pay trên Ví điện tử Momo](https://laptrinhvb.net/uploads/source/vbnet/qrcode_pay_csharp_thumb.png)
![[C#] Liệt kê danh sách font chữ được cài đặt vào máy tính](https://laptrinhvb.net/uploads/source/image_baiviet/c7bd5b3a8caeed378ecdd9f558c0c617.jpg)
![[C#] Sự khác nhau giữa String.IsNullOrEmpty và String.IsNullOrWhiteSpace](https://laptrinhvb.net/uploads/source/new_image_baiviet/string_isnull_or_empty.png)
![[C#] Xuất dữ liệu và vẽ đồ thị Chart từ GridView sang Excel](https://laptrinhvb.net/uploads/source/csharp/char_excel_thumb.png)
![[C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/microsoft-authenticator-cover.jpg)
![[C#] Hướng dẫn đổi tên Namespace trên Visual Studio Winform](https://laptrinhvb.net/uploads/source/devexpress/name_space_rename.jpg)
![[C#] Hướng dẫn viết ứng dụng ẩn và hiện Taskbar Windows](https://laptrinhvb.net/uploads/source/image_baiviet/ce1585bdf428bd1d3f0ae16de0a25b65.png)
![[C#] Hướng dẫn lấy khoảng thời gian ngày đến ngày trong tuần lập trình csharp](https://laptrinhvb.net/uploads/source/csharp/week_csharp_thumb.jpg)
![[C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/momo_danang.png)
![[FRAMEWORK] Giới thiệu Framework  UI Control thiết kế giao diện Winform C#](https://laptrinhvb.net/uploads/source/image_baiviet/c5b252596b59de95b024d1faf308d404.png)
![[C#] Cách tính toán số lớn vượt giới hạn sử dụng thư viện Big Number](https://laptrinhvb.net/uploads/source/csharp/Bignumber_thumb.png)
![[C#] Chia sẽ công cụ component Dropdown Extend Winform](https://laptrinhvb.net/uploads/source/csharp/drop_down_ex_thumb.png)
![[C#] Bảo mật source code .NET sử dụng VaultVM Tool](https://laptrinhvb.net/uploads/source/new_image_baiviet/vaultVM_tools.png)

![[C#] Hướng dẫn gộp tất cả các file thư viện dll và file chạy exe thành một file exe duy nhất sử dụng Microsoft ILMerge](https://laptrinhvb.net/uploads/source/csharp/ilmerge.png)
![[C#] Hướng dẫn sử dụng thư viện FluentValidation để kiểm tra Form nhập liệu winform](https://laptrinhvb.net/uploads/source/csharp/fluent_validate_csharp.png)
![[C#] Sử dụng thư viện AltoHTTP để Download file Start, Pause, Resume đơn giản](https://laptrinhvb.net/uploads/source/csharp/donwload_file_lib_csharp_demo.gif)
![[C#] Source code lấy thông tin phần cứng Hardware của máy tính](https://laptrinhvb.net/uploads/source/csharp/hardware_csharp_thumb.jpg)
![[C#] LINQ trong CSharp - Phần một](https://laptrinhvb.net/uploads/source/image_baiviet/833b1af041117b932108bd2ed9e3fc96.jpg)
![[C#] Hướng dẫn lấy thông tin tỷ giá vàng SJC sử dụng API](https://laptrinhvb.net/uploads/source/vbnet/ty_gia_vang.png)
![[C#] Hướng dẫn fixed lỗi xóa hình ảnh trong csharp](https://laptrinhvb.net/uploads/source/image_baiviet/6d82d92f7643b32d76ae2c4cfd9ab231.png)
