English 中文(简体)
C# TraceSource class in multithreaded application
原标题:

msdn: "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." it contains only instance methods.

How should I use it in a way that all activity gets recorder by TextWriterTraceListener to a text file. Is one static member which all threads use (by calling) TraceEvent-method safe.

(I ve kind of asked this question in how to instantiate C# TraceSources to log (multithreaded) ASP.NET 2.0 Web application (asmx ws)?, but I cannot just believe if somebody just says it s OK despite the documentation).

最佳回答

The TraceSource class is thread safe. See http://msdn.microsoft.com/en-us/library/system.diagnostics.tracesource.aspx.

I believe that previously it wasn t listed as thread safe but that was a documentation bug.

问题回答

When using a resource that isn t thread safe (or isn t guaranteed to be thread safe) in a multi-threaded app I ll use the lock keyword

lock( _lockObject)
{
  // do my non-thread-safe-operations here
}

Documentation/examples for the lock keyword can be found here:

http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx

As for all threads using a common static instance of the class - that isn t necessarily thread safe. Using a static instance ensures that the state of the object is shared/consistent among the threads but desn t necessarily prevent parallel calls to the method. In fact, if the instance methods are making use of some common state variables within the static object - accessing the object from multiple threads could introduce additional problems - race conditions, multiple threads attempting to access the same resource, etc. A static instance of an object does not prevent these conditions.

If you are using a static instance of the object, put a lock around it when you are operating on it and you should be okay.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签