0

my application contain Listbox that i am add files into and when i do it with Add folder it check all the files inside this folder and do it by open different thread on each file and then add those files into the Listbox - so should i use lock to prevent case that 2 (or more) files will try to add file at same time ?

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();
    }
}
user1269592
  • 691
  • 3
  • 12
  • 24
  • 2
    You cant access a UI control from a seperate thread anyway. Not sure why you would try. Whats the reasoning behind it? – Steve Dec 22 '12 at 07:18
  • Can you post your code on how you are using multiple threads to access your files and your UI control at the same time? – Ravi Y Dec 22 '12 at 08:50

1 Answers1

2

IIRC (No C# for long time for me) :)
The completed event of the background worker is raised on the UI thread.
So basically, different completion events will be ran sequential - and no need for lock.

See BackgroundWorker RunWorkerCompleted Event

Community
  • 1
  • 1
Itay Karo
  • 17,924
  • 4
  • 40
  • 58