Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I understand you want only single background worker launched at time. If so, then try this (based on <a href="http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx" rel="noreferrer">System.Threading.AutoResetEvent</a>)</p> <pre><code>//introduce additional field private AutoResetEvent _workerCompleted = new AutoResetEvent(false); //modify StartBackgroundFileChecker private void StartBackgroundFileChecker(string file) { ListboxFile listboxFile = new ListboxFile(); listboxFile.OnFileAddEvent += listboxFile_OnFileAddEvent; BackgroundWorker backgroundWorker = new BackgroundWorker(); backgroundWorker.WorkerReportsProgress = true; backgroundWorker.DoWork += (s3, e3) =&gt; { //check my file }; backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; backgroundWorker.RunWorkerAsync(); //new code - wait for completion _workerCompleted.WaitOne(); } //add completion notification to backgroundWorker_RunWorkerCompleted void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (Interlocked.Decrement(ref numWorkers) == 0) { //update my UI } //new code - notify about completion _workerCompleted.Set(); } </code></pre> <p>In that solution your background thread will start new BackgroundWorker one by one - this can be not optimal (you could avoid BackgroundWorker at all and simply update UI via Dispatch in <em>threadStart</em> delegate)</p> <p>In my opinion better to control number of parallel threads and still process files in multiple but limited numbers of threads.</p> <p>Here is the alternative solution (based on System.Threading.Tasks namespace):</p> <pre><code> private void btnAddDir_Click(object sender, EventArgs e) { var selectedPath = folderBrowserDialog1.SelectedPath; Task.Factory.StartNew(() =&gt; { var files = Directory.EnumerateFiles(selectedPath, "*.*", SearchOption.AllDirectories); Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 10 // limit number of parallel threads here }, file =&gt; { //process file here - launch your process }); }).ContinueWith( t =&gt; { /* when all files processed. Update your UI here */ } ,TaskScheduler.FromCurrentSynchronizationContext() // to ContinueWith (update UI) from UI thread ); } </code></pre> <p>You can tweak this solution for your specific needs.</p> <p>Used classes/methods (see MSDN for reference):</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" rel="noreferrer">Task</a></li> <li>TaskScheduler.FromCurrentSynchronizationContext </li> <li>Parallel.ForEach Method (IEnumerable, ParallelOptions, Action)</li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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