I’m looking for a way of checking when all threads in threadpool have finished their tasks. Currently I’m using a counter that decrements when a thread has finished it’s job and when counter == 0 I’m invoking my WorkComplete method. This seems to work but when i get to the final ‘job’ it doesn’t appear to process the result? Or at least the UI doesn’t get it. Here is what i currently have:
Queuing work items + incrementing counter
foreach (string s in URLs)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), s);
Interlocked.Increment(ref counter);
}
Do Work:
public void DoWork(object sender)
{
lock (_threadLock)
{
try
{
string url = (string)sender;
result.URL = url;
if (chkFb.Checked)
{
result.Shares = grabber.GetFacebookShares(url);
}
if (chkTwitt.Checked)
{
result.Tweets = grabber.GetTweetCount(url);
}
if (chkPlusOne.Checked)
{
result.PlusOnes = grabber.GetPlusOnes(url);
}
Interlocked.Decrement(ref counter);
this.Invoke(new ThreadDone(ReportProgress), result);
}
catch (Exception exc)
{
MessageBox.Show(string.Format("Errror: {0}", exc.Message);
}
finally
{
if (counter == 0)
{
this.Invoke(new ThreadDone(ReportProgress), result);
this.Invoke(new Complete(WorkComplete));
}
}
}
}
But the amount of URL’s processed is always one less than the total count, it’s almost like the last thread isn’t ‘Reporting back’ or something. Does anyone have any ideas?
Thankyou
There are a few issues in the above code:
Interlocked.Decrementisn’t in the finally block. This means that an exception will prevent thejobCounterfrom getting reduced properly._threadLock). If this is desired, there’s no reason to use multiple thread pool threads – just have one thread process all of the items in a loop, since thats effectively what you’re doing now.chkTwitt.Checked). This isn’t reliable.resultvariable, which is shared across all of the threads. Its not clear how this would be used, in a practical sense.Given that you’re effectively just processing a collection of items (URLs), you may want to also consider using
Parallel.ForEachfor evenPLINQto process the items.