title: Index was out of range wpf c#
body: <p>I have a function that gets the running apps every 10 seconds, place them on a listbox and sends them to the other window if you click the send button. Now the problem is whenever I try to open then immediately close an app, it would send an error pointing to my list.
  I'm not sure what to do here. </p>
  
  <pre><code> Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.
  </code></pre>
  
  <p>Here's my code if in case it brings any help:</p>
  
  <pre><code>private List&lt;int&gt; listedProcesses = new List&lt;int&gt;();
      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 &lt; 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 =&gt; 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&lt;ProcessInfoItem&gt; piis = new List&lt;ProcessInfoItem&gt;();
      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&lt;ProcessInfoItem&gt; GetAllProcessInfos()
      {
  
          List&lt;ProcessInfoItem&gt; result = new List&lt;ProcessInfoItem&gt;();
          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;
          }
      }
  </code></pre>