首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >清除列表-清除的数据返回。

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

Stack Overflow用户
提问于 2013-01-10 13:57:55
回答 1查看 134关注 0票数 0

我正在构建应用程序谁播放Wireshark文件和发送数据包到网卡使用pcapdot.net。

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

代码语言:javascript
运行
复制
List<string> capturesList = null;
    string pathToSearch = null;
    DialogResult dialog;
lvFiles is my ListView

添加目录按钮

代码语言:javascript
运行
复制
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文件:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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();
}

启动播放按钮:

代码语言:javascript
运行
复制
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.RunWorkerCompleted中之后,我将清除所有列表,并再次从目录根目录和playFiles中添加文件:

代码语言:javascript
运行
复制
        capturesList.RemoveRange(0, capturesList.Count);
        lvFiles.Items.Clear();
        addFiles();
        playFiles();
EN

回答 1

Stack Overflow用户

发布于 2013-01-10 14:38:19

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

代码语言:javascript
运行
复制
    lock (capturesList.SyncRoot) {

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

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14259603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档