清除列表 - 清除的数据返回

时间:2013-01-10 13:57:56

标签: c# winforms

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

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

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

和添加文件:

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

将文件添加到我的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();

}

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

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

1 个答案:

答案 0 :(得分:0)

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

    lock (capturesList.SyncRoot) {

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

    }
相关问题