views:

566

answers:

3

I have a vb.net application that uses threads to asynchronously process some tasks in a "Scheduled Task" (console application).

We are limiting this app to run 10 threads at once, like so:

(pseudo-code) - create a generic list of 10 threads - spawn off the threadproc for each one - do a thread.join statement for each thread to wait for the longest running one to complete.

What i am finding is that if the code called by the threadproc contains any "Debug.Writeline" or "Trace.Traceinformation" statements, the thread hangs. I can see the thread in the Debug - Windows - Threads window, and switch to it, but it highlights the debug.writeline statement and never gets past it.

So my question is, is there something special about the Debug or Trace statements that make them non-thread-safe? Any idea why this would hang things up? If I leave the debug statement in, the thread never completes. If I take the debug statement out, the thread completes in less than 5 seconds.

Thanks in advance :)

A: 

This is a guess but do you have a trace listener that maybe is interfering with the Debug.WriteLine?

Jeff Widmer
+3  A: 

Yes and no.

Internally, Debug.WriteLine ends up calling into TraceInternal.WriteLine. This particular function does not explicitly stop thread execution but it does acquire a process global lock during the execution of the method. This lock protects both the list of trace listeners and serializes the processing of WriteLine commands.

It's possible for 2 threads to simultaneously hit this WriteLine statement and hence have one thread pause for a short period of time. It is also possible for a custom trace listener to be doing a very long lived or blocking operation which would essentially freeze all other threads for a noticable period of time.

Use Visual Studio to check and see what other threads are currently broken in this function. See if that gives you a clue as to what is holding up this process.

JaredPar
A: 

jeff, jared

thanks much! after reading your suggestions, there is a custom trace listener in this app. Once I commented it out, my locking problems were solved. Now if I could only track down the original dev to find out what they were doing with this customer listener...

Thanks again,

Jim

Jim