How To Derive from an AsyncInit Base Class |
This topic details the steps necessary for defining a type that derives from an AsyncInit base class.
Determine the types of the initialization arguments (if any), e.g. IProgress<long>.
Import the AsyncInit namespace:
using Ditto.AsyncInit;
Derive from the corresponding AsyncInitBase or CancelableAsyncInitBase(recommended), specifying your class as the first generic type argument:
class UniversalAnswerService : CancelableAsyncInitBase<UniversalAnswerService, IProgress<long>> { public int Answer { get; private set; } }
Implement a private parameterless constructor, passing null as parameter to base:
private UniversalAnswerService() : base(null) { }
Override InitAsync():
protected override async Task InitAsync(IProgress<long> progress, CancellationToken cancellationToken) { await Task.Delay(TimeSpan.FromDays(7500000 * 365.25), cancellationToken); Answer = 42; }
var service = await UniversalAnswerService.CreateAsync(progress, cancellationToken); var answer = service.Answer;