none
如何实现是实时更新列表 RRS feed

  • 问题

  • 当有个进程启动,listview中立即就自动添加新的进程进去,如何实现?我原先的做法是用一个timer进行固定时间的重载listview,时间设置短点勉强算是实时吧,但是有个问题就是listview会重新刷新一样,原先选中项目也不选中了还得重新选,如果刷新过快就会根本没法选中任何选项了。请教高手这个问题该当如何解决?谢谢了!
    Yang
    2011年2月12日 2:20

答案

  •   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Clear()
        Dim CurrentProcess As Process
        For Each CurrentProcess In Process.GetProcesses
          ListBox1.Items.Add(CurrentProcess.ToString)
        Next
        Timer1.Start()
        Timer1.Interval = 100
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim CurrentProcess As Process
        For Each CurrentProcess In Process.GetProcesses
          If ListBox1.Items.Contains(CurrentProcess.ToString) = False Then
            ListBox1.Items.Add(CurrentProcess.ToString)
            MessageBox.Show("Add New Task: " & CurrentProcess.ToString)
          End If
        Next
      End Sub
    
    试下这个
    Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 zjyh16 2011年3月3日 1:45
    2011年2月18日 6:55
    版主

全部回复

  • 晕,你不要刷新就可以了,发现新项就添加,而不是整个刷新。
    2011年2月12日 12:47
  • 那请教一下如何实现发现新的进程项呢?
    Yang
    2011年2月14日 1:00
  • 你好,

    以下是我的个人意见,你可以用一个Timer控件,在Timer1_Tick事件中不停的用Process.GetProcesses()方法与当前ListBox中的每个Item比较,如果没有一个新的相同,就添加该进程。

     

    你可以先尝试下用这个试试。


    Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年2月18日 6:31
    版主
  •   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Clear()
        Dim CurrentProcess As Process
        For Each CurrentProcess In Process.GetProcesses
          ListBox1.Items.Add(CurrentProcess.ToString)
        Next
        Timer1.Start()
        Timer1.Interval = 100
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim CurrentProcess As Process
        For Each CurrentProcess In Process.GetProcesses
          If ListBox1.Items.Contains(CurrentProcess.ToString) = False Then
            ListBox1.Items.Add(CurrentProcess.ToString)
            MessageBox.Show("Add New Task: " & CurrentProcess.ToString)
          End If
        Next
      End Sub
    
    试下这个
    Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 zjyh16 2011年3月3日 1:45
    2011年2月18日 6:55
    版主
  • 谢谢!我试一下吧,好几天的问题了一直以来苦苦寻找摸索着,希望能找到答案,希望版主大人能给力地帮上大忙!
    Yang
    • 已标记为答案 zjyh16 2011年3月3日 1:44
    • 取消答案标记 zjyh16 2011年3月3日 1:45
    2011年2月18日 9:11
  •  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      ListBox1.Items.Clear()
    
      Dim CurrentProcess As Process
    
      For Each CurrentProcess In Process.GetProcesses
    
       ListBox1.Items.Add(CurrentProcess.ToString)
    
      Next
    
      Timer1.Start()
    
      Timer1.Interval = 100
    
     End Sub
    
    
    
     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
      Dim CurrentProcess As Process
    
      For Each CurrentProcess In Process.GetProcesses
    
       If ListBox1.Items.Contains(CurrentProcess.ToString) = False Then
    
        ListBox1.Items.Add(CurrentProcess.ToString)
    
        MessageBox.Show("Add New Task: " & CurrentProcess.ToString)
    
       End If
    
      Next
    
     End Sub
    
    
    试下这个
    Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    版主大大,你给的代码我试了一下,在ListBox中是可以那样子的,可是我使用的ListView,并且是Detail的试图,这种情况你给的代码中的:ListBox1.Items.Contains(CurrentProcess.ToString) = False 这一句改Listbox为ListView是显示错误的,我不知道该怎么做了?主要是对于ListView的使用不是非常熟悉。麻烦你再教一教,如何改用ListView来做这样一件事?再次谢谢,非常感谢!我把你给我的代码经过修改成下面这个,可是为什么不可以执行时没看到想要的效果,能帮我看看么?谢谢!

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListView1.Items.Clear()
    Dim CurrentProcess As Process
    Dim s As String
    For Each CurrentProcess In Process.GetProcesses
    Try
    s = CurrentProcess.ProcessName()
    Dim item1 As New ListViewItem(s)
    ListView1.Items.AddRange(New ListViewItem() {item1})
    Catch
    End Try
    Next
    Timer1.Start()
    Timer1.Interval = 100
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim CurrentProcess As Process
    Dim s As String
    For Each CurrentProcess In Process.GetProcesses
    Try
    s = CurrentProcess.ProcessName
    Dim item1 As New ListViewItem(s)
    If item1.Text.Contains(s) = False Then
    ListView1.Items.AddRange(New ListViewItem() {item1})
    End If
    Catch
    End Try

    Next
    End Sub


    Yang
    2011年2月25日 3:20
  • CurrentProcess.ToString存到List<String>,在用 它的Contains判断
    http://feiyun0112.cnblogs.com/
    2011年2月28日 7:27
    版主