Valid for blocking (AppDomain.CurrentDomain)?

I want to list all downloaded assemblies in an Asp.NET application using AppDomain.CurrentDomain.GetAssemblies() . However, when checking the documentation for AppDomain, I find the following statement:

Thread safety

All public static (Shared in Visual Basic) members of this type are thread safe. Any instance members do not guarantee thread safety.

Since GetAssemblies() is an instance method, I take this because I need to do some kind of lock around this call, if not for something else, so that someone else cannot load the new assembly into the domain, m listing the current . I would expect AppDomain to provide some kind of SyncRoot property, but it is not, and I have not found any information on the Internet on how to do this.

How do I sync this call?

Edit

  • I know that the lock statement is used to create a joint lock, for this reason I want to lock the AppDomain just like everyone else (or should), and not create my own lock that won’t prevent code that isn’t mine, from loading assemblies while I list them.
  • I know that locks that can be accepted by anyone are usually bad ideas, but I also know that not doing locks when performing unsafe operations is even worse.
  • Both answers still say that GetAssemblies () is essentially thread safe. It makes sense to me, and I really expect it to be, but how do you know that? Does anyone have a link to support this requirement? My google-fu didn't help me, and Reflector shows that this method is a thin shell around the internal inline method.
+4
source share
2 answers

SyncRoot properties in general is a very bad idea. One of the reasons why two independently developed libraries unconsciously decide to block the shared SyncRoot property and quickly enter deadlocks in the application. A lock is an operation that cannot be reliably distributed between two independent components. The best strategy here is to develop your own lock, which is used by your components to synchronize access.

In this case, although the GetAssemblies call is safe to operate from multiple threads, blocking is therefore not required. The warning you see is a general statement added to each class in BCL, unless the author has developed a thread safety type and deleted the message.

+2
source

This is a standard clause; You do not need to worry about it.
In general, as long as you (or another thread) do not modify the object, instance methods can be called from multiple threads.

Note that you should never lock the AppDomain object .

+1
source

Source: https://habr.com/ru/post/1302259/


All Articles