我正在构建应用程序谁播放Wireshark文件和发送数据包到网卡使用pcapdot.net。
在我的应用程序中,我获得了目录根目录,并且只添加了过去24小时的新文件。首先,我将所有文件从目录中添加到列表中,然后添加到我的ListView中。现在,在我的应用程序播放完我的所有文件后,我想清除我的列表,清除我的ListView,然后再次搜索过去24小时的文件,并添加到我的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();
}
将文件添加到我的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.RunWorkerCompleted中之后,我将清除所有列表,并再次从目录根目录和playFiles中添加文件:
capturesList.RemoveRange(0, capturesList.Count);
lvFiles.Items.Clear();
addFiles();
playFiles();
发布于 2013-01-10 14:38:19
尝试将线程同步添加到列表中。以下是每当迭代或修改列表时都可以使用的一个示例:
lock (capturesList.SyncRoot) {
/* perform iteration or modification */
capturesList.Add("All other threads are waiting until i am finished");
}
https://stackoverflow.com/questions/14259603
复制相似问题