Subject: Is XTrace.Listeners thread safe?
Date: 2020-08-14 20:14:14
From: mrodriguez
Source: xtrace-listeners-thread-safe
----------------------------------------------------------------------

I need to know if and where threads come into play when dealing with adding/removing listeners from the collection? Here is my code:

        Dim textWriterListener As UltimateTextWriterTraceListener = New UltimateTextWriterTraceListener(fileName)   
        If (Not XTrace.Listeners.Contains(textWriterListener)) Then
            
            XTrace.Listeners.ForEach(Sub(x As UltimateTextWriterTraceListener) x.Dispose)
            
            XTrace.Listeners.Clear
            
            XTrace.Level = TraceEventType.Verbose
            XTrace.Listeners.Add(textWriterListener)
        End If

I'm concerned about disposing of a listener that may be used by an existing thread. Does the Clear dispose of all listeners? What is the proper way to accomplish this? Will the XTrace.Listeners collection be immediately available for adding a new listener after the Clear?

----------------------------------------------------------------------

Note: This question has been asked on the Q&A forum of Thang Dang's fraudulent ComponentPro brand
If you purchased anything from ComponentPro, you have been scammed. Contact the payment processor
who sold you the license and ask for your money back.

Back to ComponentPro Q&A Forum Index