views:

131

answers:

1

I have a multi-threaded .NET application that occasionally terminates without any message. When I check the log there is an entry for an "Application Error in KERNEL32.dll". What could be causing this? Here is some basic code:

foreach (int id in ids)
{
   ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessData), id);
}

The ProcessData method looks like this:

private void ProcessData(object _id)
//Load some data from a database with id = _id
//Process that data and push it to another server using HTTP
//Increment a counter

Interlocked.Increment(ref counter);

//Update progress bar
try
{
  // Invoke the delegate on the form.
  this.Invoke(new BarDelegate(UpdateBar), counter);
}
catch {}
}

There can be millions of ids to process in some cases. But just testing with 10,000 ids usually causes the Application Error described. Am I going about this correctly?

Thank you for any help!

+1  A: 

Does QueueUserWorkItem always return true?

Also, do you get any exceptions in that empty catch?

leppie
I have solved this issue. Your comment about exceptions got me thinking and sure enough it was an unhandled exception that was being thrown which was causing this issue. Lesson learned - always handle and log exceptions in a multi-threaded application! Thank you for your input.
jensendarren
Cool and thanks :)
leppie