Question Hello! Can someone help me make an TaskManger?

Of course we can help but there's nothing to do based on your initial post. If you actually want someone to work on the project with you then there's a specific forum for that. The WinForms forum that you've posted in is for specific problems related to WinForms. What WinForms-related issue(s) are you actually having? Don't just tell us your end goal and expect us to do everything in between. Tell us exactly what you've done and exactly where you're stuck and we can try to help with that. If the issue(s) you're having are not specifically related to WinForms then you should be posting in a different forum, even if they occur in a WinForms project. If the solution would be the same in a different kind of app then it's not a WinForms issue.
 
VB.NET:
Imports System.Diagnostics
Imports System.Drawing
Imports System.Management
Imports System.Windows.Forms
Imports System.Threading.Tasks
Imports System.Security.Principal

Public Class Form1

    Private processItems As New Dictionary(Of Integer, ListViewItem)
    Private processIcons As New ImageList()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.BackColor = Color.Blue

        ListView1.View = View.Details
        ListView1.FullRowSelect = True
        ListView1.GridLines = True
        ListView1.Columns.Add("Process", 200, HorizontalAlignment.Left)
        ListView1.Columns.Add("CPU %", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("GPU %", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Memory Usage", 150, HorizontalAlignment.Left)


        ListView1.SmallImageList = processIcons


        ListView1.BackColor = Color.FromArgb(50, 50, 50)
        ListView1.ForeColor = Color.White


        Dim timer As New Timer()
        AddHandler timer.Tick, AddressOf UpdateProcessList
        timer.Interval = 3000
        timer.Start()


        UpdateProcessList()
    End Sub

    Private Async Sub UpdateProcessList()

        Dim processes = Process.GetProcesses()


        For Each p As Process In processes
            Try

                If processItems.ContainsKey(p.Id) Then
                    Dim listViewItem = processItems(p.Id)
                    Await Task.Run(Sub() UpdateProcessItem(listViewItem, p))
                Else

                    Dim listViewItem As New ListViewItem(p.ProcessName)


                    listViewItem.ImageIndex = AddProcessIcon(p)

                    listViewItem.SubItems.Add("0.0%")
                    listViewItem.SubItems.Add("0.0%")
                    listViewItem.SubItems.Add("0.0 MB")
                    listViewItem.Tag = p


                    ListView1.Items.Add(listViewItem)
                    processItems.Add(p.Id, listViewItem)
                End If
            Catch ex As Exception

            End Try
        Next

        Dim toRemove As New List(Of Integer)
        For Each kvp In processItems
            If Not processes.Any(Function(p) p.Id = kvp.Key) Then
                toRemove.Add(kvp.Key)
            End If
        Next

        For Each id In toRemove
            processItems.Remove(id)
        Next
    End Sub

    Private Sub UpdateProcessItem(listViewItem As ListViewItem, proc As Process)
        Try

            Dim cpuUsage As Double = GetCPUUsage(proc)
            Dim gpuUsage As Double = GetGPUUsage(proc)
            Dim memoryUsage As String = (proc.WorkingSet64 / 1024 / 1024).ToString("0.0") & " MB"


            listViewItem.SubItems(1).Text = cpuUsage.ToString("0.0") & "%"
            listViewItem.SubItems(2).Text = gpuUsage.ToString("0.0") & "%"
            listViewItem.SubItems(3).Text = memoryUsage
        Catch ex As Exception

        End Try
    End Sub

    Private Function AddProcessIcon(proc As Process) As Integer

        Try
            Dim icon As Icon = Icon.ExtractAssociatedIcon(proc.MainModule.FileName)

            processIcons.Images.Add(proc.Id.ToString(), icon)

            Return processIcons.Images.Count - 1
        Catch ex As Exception

            processIcons.Images.Add(SystemIcons.Information)
            Return processIcons.Images.Count - 1
        End Try
    End Function

    Private Function GetCPUUsage(proc As Process) As Double
        Try
            Using pc As New PerformanceCounter("Process", "% Processor Time", proc.ProcessName, True)
                pc.NextValue()
                Threading.Thread.Sleep(500)
                Return pc.NextValue() / Environment.ProcessorCount
            End Using
        Catch
            Return 0
        End Try
    End Function

    Private Function GetGPUUsage(proc As Process) As Double
        Try
            Dim query As String = "SELECT Name, PercentGPUTime FROM Win32_PerfFormattedData_GPUPerformanceCounters_GPUAdapterMemory"
            Using searcher As New ManagementObjectSearcher("root\CIMV2", query)
                Dim collection = searcher.Get()
                If collection.Count > 0 Then
                    For Each obj As ManagementObject In collection
                        If obj("Name").ToString().Contains(proc.ProcessName) Then
                            Return Convert.ToDouble(obj("PercentGPUTime"))
                        End If
                    Next
                End If
            End Using
        Catch ex As ManagementException

            Return 0
        Catch ex As UnauthorizedAccessException

            Return 0
        End Try
        Return 0
    End Function


    Private Sub RunAsAdminButton_Click(sender As Object, e As EventArgs) Handles RunAsAdminButton.Click

        If ListView1.SelectedItems.Count > 0 Then
            Dim proc As Process = CType(ListView1.SelectedItems(0).Tag, Process)
            Try
                Dim startInfo As New ProcessStartInfo(proc.MainModule.FileName)
                startInfo.Verb = "runas"
                Process.Start(startInfo)
            Catch ex As Exception
                MessageBox.Show("Nem lehet admin jogokkal futtatni a folyamatot: " & ex.Message)
            End Try
        End If
    End Sub


    Private Sub RestartProcessButton_Click(sender As Object, e As EventArgs) Handles RestartProcessButton.Click

        If ListView1.SelectedItems.Count > 0 Then
            Dim proc As Process = CType(ListView1.SelectedItems(0).Tag, Process)
            Try
                Dim procName = proc.ProcessName
                proc.Kill()

                Threading.Thread.Sleep(1000)


                Process.Start(procName)
            Catch ex As Exception
                MessageBox.Show("Nem lehet újraindítani a folyamatot: " & ex.Message)
            End Try
        End If
    End Sub

    Private Sub EndTaskButton_Click(sender As Object, e As EventArgs) Handles EndTaskButton.Click
        If ListView1.SelectedItems.Count > 0 Then
            Dim proc As Process = CType(ListView1.SelectedItems(0).Tag, Process)
            Try
                If Not proc.ProcessName.ToLower.Contains("system") Then
                    proc.Kill()
                    UpdateProcessList()
                Else
                    MessageBox.Show("Ez egy rendszerfolyamat, nem lehet bezárni!", "Hiba", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Catch ex As Exception
                MessageBox.Show("Nem lehet bezárni a folyamatot: " & ex.Message)
            End Try
        End If
    End Sub

    Private Sub OpenFileLocation_Click(sender As Object, e As EventArgs) Handles OpenFileLocation.Click
        If ListView1.SelectedItems.Count > 0 Then
            Dim proc As Process = CType(ListView1.SelectedItems(0).Tag, Process)
            Try
                Process.Start("explorer.exe", "/select," & proc.MainModule.FileName)
            Catch ex As Exception
                MessageBox.Show("Nem lehet megnyitni a fájl helyét.")
            End Try
        End If
    End Sub

    Private Sub SearchBox_TextChanged(sender As Object, e As EventArgs) Handles SearchBox.TextChanged
        Dim searchText As String = SearchBox.Text.ToLower()
        For Each item As ListViewItem In ListView1.Items
            item.BackColor = If(item.Text.ToLower().Contains(searchText), Color.LightBlue, Color.White)
        Next
    End Sub

    Private Sub RestartAppButton_Click(sender As Object, e As EventArgs) Handles RestartAppButton.Click
        Application.Restart()
    End Sub
End Class
 
Last edited by a moderator:
Back
Top