索引超出了wpf c#的范围

本文关键字:范围 wpf 索引 | 更新日期: 2023-09-27 18:09:40

我有一个函数,每10秒获得运行的应用程序,将它们放在一个列表框中,并发送到另一个窗口,如果你点击发送按钮。现在的问题是,每当我尝试打开然后立即关闭一个应用程序,它会发送一个错误指向我的列表。我不知道该怎么做。

 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;
        }
    }

索引超出了wpf c#的范围

您正在索引for循环引用的不同集合。听起来你可能想要:

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

。然而,你在 for循环中调用形式的函数,所以不清楚你应该迭代什么。

尝试改变,

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

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