Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Lazy<T> Constructor (LazyThreadSafetyMode)

 

Initializes a new instance of the Lazy<T> class that uses the default constructor of T and the specified thread-safety mode.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public:
Lazy(
	LazyThreadSafetyMode mode
)

Parameters

mode
Type: System.Threading::LazyThreadSafetyMode

One of the enumeration values that specifies the thread safety mode.

Exception Condition
ArgumentOutOfRangeException

mode contains an invalid value.

The thread safety mode of a Lazy<T> instance describes the behavior when multiple threads try to initialize the Lazy<T> instance.

A Lazy<T> instance that is created with this constructor does not cache exceptions. For more information, see the Lazy<T> class or the System.Threading::LazyThreadSafetyMode enumeration.

The following example demonstrates the use of this constructor to create a lazy initializer that enables multiple threads to race to create an object lazily. Multiple threads might succeed in creating instances, but all threads use the instance that was created first.

System_CAPS_noteNote

For an example that demonstrates how to use this constructor in single-threaded scenarios (specifying LazyThreadSafetyMode::None for mode), see the Lazy<T>(Boolean) constructor. For an example that demonstrates how to use this constructor to provide locking instead of race conditions in multithreaded scenarios (specifying LazyThreadSafetyMode::ExecutionAndPublication for mode), see the Lazy<T>() constructor.

The example defines a LargeObject class that will be initialized lazily by any of several threads. The three key sections of code illustrate the creation of the initializer, the actual initialization, and the constructor and finalizer of the LargeObject class. At the beginning of the Main method, the example creates the Lazy<T> object that performs lazy initialization of the LargeObject:

No code example is currently available or this language may not be supported.

The example creates and starts three threads that block on a ManualResetEvent object, so that the example can release the threads all at once. In the ThreadProc method that's used by all three threads, calling the Value property creates the LargeObject instance:

No code example is currently available or this language may not be supported.

Because the constructor for the Lazy<T> instance specified LazyThreadSafetyMode::PublicationOnly, all three threads are allowed to create LargeObject instances. The example demonstrates this by displaying console messages in the constructor and in the finalizer of the LargeObject class:

No code example is currently available or this language may not be supported.

However, the Lazy<T> object ensures that only one instance is used by all threads. The output from the example shows that all three threads use the same instance, and also shows that the other two instances can be reclaimed by garbage collection.

System_CAPS_noteNote

For simplicity, this example uses a global instance of Lazy<T>, and all the methods are static (Shared in Visual Basic). These are not requirements for the use of lazy initialization.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft