Quantcast
Viewing all articles
Browse latest Browse all 12583

background worker - many thread - for loop

Hi,

I want to create a thread inside background worker method. And This thread count will vary, depending on certain other variables.

Ina for loop, I execute my portion of code along with thread creation. I want to stop the main thread execution until the whole for loop execution is completed. 

But I faced an issue with the code. The background worker completed event was called many times, not once. This is equivalent to the for loop execution count. I dont know why this is happening?

Can some one help me to fix this?

  private void bgwIndex_DoWork(object sender, DoWorkEventArgs e)
    {
      try
      {

        foreach (string sPath in arrExtrachPaths)
        {
          DateTime t1 = DateTime.Now;
         
         ...............................
..........................................
..................................

            objThreads[iCntThread] = new Thread(new ThreadStart(oIndex[iCntThread].AddToindex_Original));
            objThreads[iCntThread].IsBackground = true;
            objThreads[iCntThread].Priority = ThreadPriority.Lowest;
            objThreads[iCntThread].Name = iCntThread.ToString();

          if (bgwIndex.CancellationPending)
          {
            e.Cancel = true;
            bIndexCancelled = true;
          }
          //}
          foreach (Thread t in objThreads)
          {
            if (t != null)
            {
              t.Start();
            }
          }

          if (bgwIndex.CancellationPending)
          {
            e.Cancel = true;
            bIndexCancelled = true;
          }
          foreach (Thread t in objThreads)
          {
            if (t != null)
              t.Join();
          }

        
        }
      }
      catch (Exception ex) 
      {
      }
    }



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>