0

私は、Wireshark ファイルを再生し、pcapdot.net を使用してパケットをネットワーク カードに送信するビルド アプリケーションです。

私のアプリケーションでは、ディレクトリ ルートを取得し、過去 24 時間の新しいファイルのみを追加します。最初に、ディレクトリからすべてのファイルを List に追加し、次に 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();
    }
}

そして追加ファイル:

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 関数を終了した後、すべてのリストをクリアし、ディレクトリ ルートと playFiles からファイルを再度追加します。

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

1 に答える 1

0

リストにスレッド同期を追加してみてください。以下は、リストを反復または変更するときに使用できる1つの例です。

    lock (capturesList.SyncRoot) {

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

    }
于 2013-01-10T14:38:19.447 に答える