2015-08-24 49 views
0

我有一个函数每10秒获取运行的应用程序,将它们放在一个列表框中,并将它们发送到另一个窗口,如果您单击发送按钮。现在,问题是每当我尝试打开然后立即关闭一个应用程序,它会发送一个指向我的列表的错误。 我不确定这里要做什么。索引超出范围wpf c#

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index. 

这里是我的代码,如果在情况下,它带来任何帮助:

private List<int> listedProcesses = new List<int>(); 
    private void SendData() 
    { 
     String processID = ""; 
     String processName = ""; 
     String processFileName = ""; 
     String processPath = ""; 
     string hostName = System.Net.Dns.GetHostName(); 

     listBox1.BeginUpdate(); 
     try 
     { 
      for (int i = 0; i < listBox1.Items.Count; i++) 
      { 
       piis = GetAllProcessInfos(); 
       try 
       { 
        if (!listedProcesses.Contains(piis[i].Id)) //place this on a list to avoid redundancy 
        { 
         listedProcesses.Add(piis[i].Id); 
         processID = piis[i].Id.ToString(); 
         processName = piis[i].Name.ToString(); 
         processFileName = piis[i].FileName.ToString(); 
         processPath = piis[i].Path.ToString(); 
         output.Text += "\n\nSENT DATA : \n\t" + processID + "\n\t" + processName + "\n\t" + processFileName + "\n\t" + processPath + "\n"; 
        } 

       } 
       catch (Exception ex) 
       { 
        wait.Abort(); 
        output.Text += "Error..... " + ex.StackTrace; 
       } 

       NetworkStream ns = tcpclnt.GetStream(); 
       String data = ""; 
       data = "--++" + " " + processID + " " + processPath + " " + processFileName + " " + hostName; 

       if (ns.CanWrite) 
       { 
        byte[] bf = new ASCIIEncoding().GetBytes(data); 
        ns.Write(bf, 0, bf.Length); 
        ns.Flush(); 
       } 
      } 
     } 
     finally 
     { 
      listBox1.EndUpdate(); 
     } 
    } 

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     ProcessInfoItem pii = piis.FirstOrDefault(x => x.Id == (int)(sender as ListBox).SelectedValue); //setting value for list box 
     if (pii != null) 
     { 
      string hostName = System.Net.Dns.GetHostName(); 

      textBox4.Text = listBox1.SelectedValue.ToString(); 
      textBox5.Text = (pii.FileName); 
      textBox6.Text = (pii.Path); 
      textBox7.Text = hostName; 
     } 
    } 

    private List<ProcessInfoItem> piis = new List<ProcessInfoItem>(); 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     piis = GetAllProcessInfos(); 
     listBox1.DisplayMember = "Name"; 
     listBox1.ValueMember = "Id"; 
     listBox1.DataSource = piis; 
     textBox1.Text = GetIpAdd().ToString(); 
    } 
    private List<ProcessInfoItem> GetAllProcessInfos() 
    { 

     List<ProcessInfoItem> result = new List<ProcessInfoItem>(); 
     Process currentProcess = Process.GetCurrentProcess(); 
     Process[] processes = Process.GetProcesses(); 
     foreach (Process p in processes) 
     { 
      if (!String.IsNullOrEmpty(p.MainWindowTitle)) 
      { 
       ProcessInfoItem pii = new ProcessInfoItem(p.Id,p.MainModule.ModuleName, p.MainWindowTitle, p.MainModule.FileName); 
       result.Add(pii); 
      } 
     } 
     return result; 
    } 
    public class ProcessInfoItem 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string FileName { get; set; } 
     public string Path { get; set; } 
     public ProcessInfoItem(int id, string name, string filename, string path) 
     { 
      this.Id = id; 
      this.Name = name; 
      this.FileName = filename; 
      this.Path = path; 
     } 
    } 
+2

它应该是 “listBox1.Items.Count - 1” 在你的for循环 –

+8

@KarthikGanesan OP使用'<'所以'计数'没问题。 –

+0

为什么你认为'piis'的上限不会大于'listBox1.Items.Count' –

回答

4

您索引通过不同的集合,你for循环引用。这听起来像你可能想要的:

piis = GetAllProcessInfos(); 
for (int i = 0; i < piis.Count; i++) 
{ 

改为。但是,您在for循环内调用的函数形式,因此不清楚应该迭代哪些内容。

+0

这工作完美。谢谢先生 – charlie9495

0

尝试改变,

for (int i = 0; i < listBox1.Items.Count; i++) 
     { 
      piis = GetAllProcessInfos(); 

piis = GetAllProcessInfos(); 
for (int i = 0; i < piis.Count; i++) 
     {