清除一个列表—清除的数据返回

本文关键字:清除 数据 返回 列表 一个 | 更新日期: 2023-09-27 17:51:16

我正在构建一个应用程序,它可以播放Wireshark文件,并使用pcapdot.net将数据包发送到网卡。

在我的应用程序中,我获得目录root并仅添加过去24小时内的新文件。首先,我将所有文件从目录添加到列表,然后添加到我的ListView。现在,在我的应用程序完成播放我的所有文件后,我想清除我的列表,清除我的ListView并再次搜索过去24小时内的now文件并添加到我的ListView。我的问题是,如果我选择有2个文件的文件夹,添加了2个文件,在应用程序完成播放这2个文件后,我的List<string>ListView现在是空的,应用程序现在不仅添加了这2个文件,而且这个文件多次添加,我找不到问题所在。

List<string> capturesList = null;
    string pathToSearch = null;
    DialogResult dialog;
lvFiles is my ListView

添加目录按钮

private void btnAddDir_Click(object sender, EventArgs e)
{
    string fileToAdd = string.Empty;
    capturesList = new List<string>();
    dialog = folderBrowserDialog1.ShowDialog();
    tbAddDir.Text = folderBrowserDialog1.SelectedPath;
    pathToSearch = folderBrowserDialog1.SelectedPath;
    if (dialog == DialogResult.OK)
    {
        toolStripStatusLabel.Text = "Search for pcap files...";
        groupBoxRootDirectory.Enabled = false;
        btnStart.Enabled = false;
        addFiles();
    }
}

和add文件:

private void addFiles()
{
    BackgroundWorker backgroundWorker = new BackgroundWorker();
    backgroundWorker.WorkerReportsProgress = true;
    backgroundWorker.DoWork += new DoWorkEventHandler(
    (s3, e3) =>
    {
        capturesList = SafeFileEnumerator.EnumerateFiles(pathToSearch, "*.pcap",
            SearchOption.AllDirectories).ToList();
    });
    backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        (s3, e3) =>
        {
            if (capturesList.Count != 0)
                AddFilesToListView(capturesList);
        });
    backgroundWorker.RunWorkerAsync();
}

add files to my ListView:

private void addFileToListBox(string filePath, string duration)
{
    ListViewItem item = new ListViewItem(new string[] { new FileInfo(filePath).Name, duration, "Waiting" });
    item.Tag = new FileInfo(filePath).FullName;
    this.Invoke((MethodInvoker)delegate { lvFiles.Items.Add(item); });
}
private void AddFilesToListView(List<string> filesList)
{
    string fileToAdd = string.Empty;
    Editcap editcap = new Editcap();
    Capinfos capinfos = new Capinfos();
    BackgroundWorker backgroundWorker = new BackgroundWorker();
    backgroundWorker.WorkerReportsProgress = true;
    backgroundWorker.DoWork +=
        (s1, e1) =>
        {
            for (int i = 0; i < filesList.Count; i++)
            {
                FileInfo fileInfo = new FileInfo(filesList[i]);
                if (checkFileCreationDate(fileInfo))
                {
                    if (editcap.isWiresharkFormat(fileInfo.FullName))
                    {
                        if (editcap.isLibpcapFormat(fileInfo.FullName))
                        {
                            addFileToListBox(fileInfo.FullName, capinfos.getFileDuration(fileInfo.FullName));
                        }
                        else if (!editcap.isLibpcapFormat(fileInfo.FullName))
                        {
                            fileToAdd = editcap.getNewFileName(fileInfo.FullName);
                            if (new FileInfo(fileToAdd).Exists)
                            {
                                addFileToListBox(fileToAdd, capinfos.getFileDuration(fileToAdd));
                            }
                        }
                    }
                }
            }
        };
    backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
    (s1, e1) =>
    {
    });
    backgroundWorker.RunWorkerAsync();
}

开始播放按钮:

private void btnStart_Click(object sender, EventArgs e)
{
    playFiles();
}
playFiles function:
private void playFiles()
{
    lockButtons();
    string filePath = string.Empty;
    BackgroundWorker backgroundWorker = new BackgroundWorker();
    backgroundWorker.WorkerReportsProgress = true;
    backgroundWorker.DoWork += new DoWorkEventHandler(
    (s3, e3) =>
    {
        for (int i = 0; i < lvFiles.Items.Count && shouldContinue; i++)
        {
            PcapFile pcapFile = new PcapFile();
            pcapFile.sendQueue(filePath, adapter);
        }
    });
    backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        (s3, e3) =>
        {
            capturesList.RemoveRange(0, capturesList.Count);
            lvFiles.Items.Clear();
            addFiles();
            playFiles();
        });
    backgroundWorker.RunWorkerAsync();
}

后,我完成了playFiles功能内backgroundWorker。我清除了所有的列表,并再次从我的目录root和playFiles中添加文件:

        capturesList.RemoveRange(0, capturesList.Count);
        lvFiles.Items.Clear();
        addFiles();
        playFiles();

清除一个列表—清除的数据返回

尝试将线程同步添加到列表中。下面是一个可以在迭代或修改列表时使用的示例:

    lock (capturesList.SyncRoot) {
        /* perform iteration or modification */
        capturesList.Add("All other threads are waiting until i am finished");          
    }