"is .net system.net.cookiecontainer thread safe?" Code Answer


no, not all .net classes are thread safe. in fact, very few have a need to be. in general, static members should be thread-safe, but that is about it.

immutable / semi-immutable objects are automatically thread safe (this includes things like xsltransform etc) - and there are a mutable few cases (such as threaded containers) where you can expect things to be thread safe. msdn states thread-safety for each class.

i would have no expectation for a cookie-container to be thread-safe, so you will probably have to synchronize this yourself.


re your second point; exactly which variables are you thinking of? your own local state variables won't be directly updated during the async request, so it simply falls to you to synchronize access when preparing requests are when processing responses. most commonly, via a monitor - i.e.

lock(synclock) {
    // prepare request from (synchronized) state

and then in the callback

lock(synclock) {
    // ...read values from request...
    // ...update local state...

where synclock is just a lock object (perhaps held against an instance):

private readonly object synclock = new object();
By TLGreg on July 8 2022

Answers related to “is .net system.net.cookiecontainer thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.