Handling static objects
When working with static data in .NET, there are some important things to understand when it comes to managed threading.
Static data and constructors
One important item to understand about accessing static data from managed threads relates to constructors. Before a static member of any class can be accessed, its static constructor must first finish running. The runtime will block thread execution until the static constructor has run to ensure that all required initialization has finished.
If you are using static objects within your own code base, you will know which classes have static constructors and can control the complexity of the logic inside them. When the static data is outside of your control, inside a third-party library or .NET itself, things may not be so clear.
Let’s try a quick example to illustrate the potential delays that can be encountered in this scenario.
- Start by creating a new .NET console application in Visual...