如何使用正在运行的进程实时更新列表框?



在Windows中,任务管理器会自动更新笔记本电脑/台式机上所有正在运行的进程。但是,这不是我的意思。

我的问题是,有没有办法使用所有新进程更新 ListBox(最好是Timer1.Tick由于我的实例(,但要实时更新(每隔设定的时间间隔更新新进程(?

我的ListBox1充满了当前正在运行的进程。

我尝试过的事情

  1. 在我的子"Timer1_Tick"中,我尝试使用ListBox1.Refresh()代码,但是,我意识到,所做的只是刷新ListBox而不是正在运行的进程。

  2. 研究类似问题

获取正在运行的进程的代码

Private Sub Mainframe_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim procs() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses
Dim f As String
For Each proc As System.Diagnostics.Process In procs
f = GetProcessFileName(proc)
If f.Length > 0 Then
ListBox1.Items.Add(f)
ListBox1.Items.Add("MD5: " & GetMD5String(f)) <-- Not relevant
ListBox1.Items.Add(String.Empty)
End If
Next
End Sub

我的Timer1代码

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False 'Stops the timer
ListBox1.Update()
ListBox1.Refresh()
Timer1.Enabled = True
End Sub

从我的头顶:应该是这样的。

免责声明:我说的不是那么好的VB;-(

PS:可能你得到一个"不在同一线程上调用异常">

Private Sub Mainframe_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'call it initially
RefreshList()
End Sub
'this sub actually does the lisbox update
Private Sub RefreshList()
Dim procs() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses
Dim f As String
'as from @Visual Vincent's comment:
ListBox1.BeginUpdate()
ListBox.Items.Clear()
For Each proc As System.Diagnostics.Process In procs
f = GetProcessFileName(proc)
If f.Length > 0 Then
ListBox1.Items.Add(f)
ListBox1.Items.Add("MD5: " & GetMD5String(f)) <-- Not relevant
ListBox1.Items.Add(String.Empty)
End If
Next
ListBox1.EndUpdate()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False 'Stops the timer
RefreshList()
Timer1.Enabled = True
End Sub

最新更新