首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果我通过线程将文件添加到我的列表框,我应该使用Lock吗?

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

Stack Overflow用户
提问于 2012-12-22 15:15:54
回答 1查看 130关注 0票数 0

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

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-22 17:37:47

IIRC (我很长一段时间没有C# ) :)

后台工作线程的completed事件在UI线程上引发。

因此,基本上,不同的完成事件将按顺序运行-并且不需要锁定。

请参阅BackgroundWorker RunWorkerCompleted Event

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

https://stackoverflow.com/questions/14000865

复制
相关文章

相似问题

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