Note that there are some explanatory texts on larger screens.

plurals
  1. POShould i use Lock if i add files to my Listbox via threads?
    primarykey
    data
    text
    <p>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 ?</p> <pre><code>private void btnAddDir_Click_1(object sender, EventArgs e) { int totalCount = 0; int count = 0; string fileToAdd = string.Empty; List&lt;string&gt; filesList = new List&lt;string&gt;(); 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) =&gt; { 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) =&gt; { }); backgroundWorker.ProgressChanged += (s1, arguments) =&gt; { listBoxFiles.Items.Add(arguments.UserState); }; backgroundWorker.RunWorkerAsync(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload