繁体   English   中英

如果我通过线程将文件添加到我的列表框中,我应该使用Lock吗?

[英]Should i use Lock if i add files to my Listbox via threads?

我的应用程序包含我添加文件的Listbox,当我使用Add文件夹时,它会检查此文件夹中的所有文件并通过在每个文件上打开不同的线程然后将这些文件添加到列表框中来执行 - 所以我应该使用锁定防止2(或更多)文件同时尝试添加文件的情​​况?

private void btnAddDir_Click_1(object sender, EventArgs e)
{
    int totalCount = 0;
    int count = 0;
    string fileToAdd = string.Empty;
    List<string> filesList = new List<string>();
    BackgroundWorker backgroundWorker = null;
    DialogResult dialog = folderBrowserDialog1.ShowDialog();
    if (dialog == DialogResult.OK)
    {
        btnAddfiles.Enabled = false;
        btnAddDir.Enabled = false;
        btnPlay.Enabled = false;
        Editcap editcap = new Editcap();

        foreach (string file in SafeFileEnumerator.EnumerateFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories))
        {
            if (editcap.isWiresharkFormat(file))
            {
                filesList.Add(file);
                totalCount++;
            }
        }

        backgroundWorker = new BackgroundWorker();
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.DoWork +=
            (s1, e1) =>
            {
                foreach (string fileName in filesList)
                {
                    if (editcap.isWiresharkFormat(fileName))
                    {
                        if (editcap.isLibpcapFormat(fileName))
                        {
                            backgroundWorker.ReportProgress(0, fileName);
                            count++;
                        }
                        else if (!editcap.isLibpcapFormat(fileName))
                        {
                            fileToAdd = editcap.getNewFileName(fileName);

                            if (new FileInfo(fileToAdd).Exists)
                            {
                                backgroundWorker.ReportProgress(0, fileToAdd);
                                count++;
                            }
                        }

                        this.Invoke((MethodInvoker)delegate
                        {
                            labelStatus.Text = string.Format("Please wait..({0}/{1} files were added)", count.ToString("#,##0"), totalCount.ToString("#,##0"));
                            if(listBoxFiles.Items.Count != 0)
                                listBoxFiles.SetSelected(listBoxFiles.Items.Count - 1, true);
                        });
                    }
                }
            };

        backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        (s1, e1) =>
        {

        });

        backgroundWorker.ProgressChanged +=
         (s1, arguments) =>
         {
             listBoxFiles.Items.Add(arguments.UserState);
         };

        backgroundWorker.RunWorkerAsync();
    }
}

IIRC(很长一段时间没有C#):)
在UI线程上引发了后台工作程序的已完成事件。
所以基本上,不同的完成事件将按顺序运行 - 而不需要锁定。

请参见BackgroundWorker RunWorkerCompleted事件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM