I started a Thread which collects around >500 records every 5 minutes.Then by foreach statement i created seperate background worker for each details obtained.I collect the data by checking the condition which is in Queue(‘QU’ in Db).While backgroundworkers Do_Work event i update the ‘QU’ status to ‘PR’.
BackgroundWorker bw1;
Collection collect = new Collection()
foreach (Transaction trans in transactionList)
{
try
{
collect.TransactionDetails = trans;
bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bw1.RunWorkerAsync(collect);
}
catch
{ }
}
This is lengthy process so its must to run using backgroundworker.
My question is if this process takes more than Thread Sleep time to complete then same records with ‘QU’ status will be collected again.How can i understand the process is still running and make the Thread to wait for some more time till complete
You need to somehow keep track of the running processes. The most basic way to do this would be to use a counter variable that you increment on creating a
BackgroundWorkerand decrement when it’s completed. You could then find out the number of still runningBackgroundWorkers by simply looking at the variable’s value.If you’re on .NET 4, you also might want to have a look at the Task Parallel Library.
Another thing: As far as i understand it you create a worker thread for over 500 items? That seems like a lot to me. Consider partitioning the workload into fewer larger pieces.