Ashley John
Ashley John

Reputation: 2453

Lazy<T> .Subsequent threads to initialize if one thread fails

I am using Lazy<T> with LazyThreadSafetyMode as ExecutionAndPublication and using an initialization method.

What I would like to achieve is if initialization fails on one thread ,only then the next thread should try and initialize it again. I dont want all threads to race for initialization (as in Publication only mode) or the exception to be cached and thrown again for all threads (as in ExecutionAndPublication mode).

Is there a way to achieve this?

Upvotes: 1

Views: 555

Answers (1)

Che
Che

Reputation: 489

As explained here

As noted in the previous section, Lazy objects created by specifying LazyThreadSafetyMode.PublicationOnly treat exceptions differently. With PublicationOnly, multiple threads can compete to initialize the Lazy instance. In this case, exceptions are not cached, and attempts to access the Value property can continue until initialization is successful.

Upvotes: 2

Related Questions