ThreadStatic v.s. ThreadLocal: is generic better than attribute?

Something the blog post noted in the comments doesn’t make explicit, but I find to be very important, is that [ThreadStatic] doesn’t automatically initialize things for every thread. For example, say you have this: [ThreadStatic] private static int Foo = 42; The first thread that uses this will see Foo initialized to 42. But subsequent … Read more

Initializing ThreadStatic field still causes NullReferenceException

Initializing ThreadStatic fields is a little tricky. In particular there is this caveat: Do not specify initial values for fields marked with ThreadStaticAttribute, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. in the MSDN Docs. What this means is that the thread running when the class … Read more

How does the ThreadStatic attribute work?

How does [ThreadStatic] attribute work? You can think that the field marked with ThreadStatic is attached to a thread and its lifetime is comparable to the lifetime of a thread. So in pseudocode ThreadStatic is similar (by semantics) to having a key-value attached to a thread: Thread.Current[“MyClass.myVariable”] = 1; Thread.Current[“MyClass.myVariable”] += 1; but the syntax … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)