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