This project has moved. For the latest updates, please go here.

Lazy NotifyTaskCompletion

May 15, 2014 at 5:51 PM
Hi Stephen,

It probably feels trite after so many thank yous, but let me just give my thanks again for the the commitment and contributions you continuously put into the community.

I have been playing with combining aspects of the AsyncLazy type and the NotifyTaskCompletion type, to enable a NotifyTaskCompletion to be lazily created from a factory if desired.

The main changes are replacing the Task<T> property with a Lazy<Task<T>>, implementing additional constructors similar to the AsynLazy class, and only attaching the notification continuation when the Result is requested.

My questions are: Is this a good idea? Have you ever implemented something similar? Is there a better way to handle this using other aspects of AsyncEx?

Kind regards,
May 19, 2014 at 2:37 PM
I have not used a lazy NotifyTaskCompletion. I suppose it would be useful in some cases.

Your approach sounds fine, though the Lazy type is intended to resolve multi-threaded initialization race conditions, and that shouldn't be necessary for NTCs. (I'm assuming that you treat your VMs as though they were thread-affine; that's the policy I always follow). So, a lazy initialization could be simply this:
private INotifyTaskCompletion<T> _myProperty;
public INotifyTaskCompletion<T> MyProperty
    if (_myProperty == null)
      _myProperty = NotifyTaskCompletion.Create(...);
    return _myProperty;
However, if you do incorporate that logic into the NotifyTaskCompletion type like you're thinking of doing, the boilerplate property getter code would be shorter.