How to initialize ThreadStatic fields?

  • Thread starter Thread starter alexey_r
  • Start date Start date
A

alexey_r

I need to set the value of a ThreadStatic field before starting the
thread. According to MSDN, setting the value normally won't work,
because this is done only once. Neither will a static constructor.
Checking for null every time the field is accessed is too slow. The
only idea I have is to use a second, bool, ThreadStatic field to tell
me if the first field has been initialized. Is there a better way?
 
Alexey,

No, there isn't another way. You will have to check every time that you
want to access it.

You can easily encapsulate this logic in a property.

Hope this helps.
 
Thank you!
Alexey,

No, there isn't another way. You will have to check every time that you
want to access it.

You can easily encapsulate this logic in a property.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I need to set the value of a ThreadStatic field before starting the
thread. According to MSDN, setting the value normally won't work,
because this is done only once. Neither will a static constructor.
Checking for null every time the field is accessed is too slow. The
only idea I have is to use a second, bool, ThreadStatic field to tell
me if the first field has been initialized. Is there a better way?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top