protected override void Execute(NativeActivityContext context) { DateTime expTime = this.ExpirationTime.Get(context); // DurableTimerExtension.Register takes a TimeSpan so we use this method to convert between // an the expired DateTime and how long of a delay to register for the timer TimeSpan delay = expTime - DateTime.Now; // If the input DateTime is already past due, then there is no need to register a timer. if (delay > TimeSpan.Zero) { // Get the timer extension. Since we are asking for a type of TimerExtension and not specifically DurableTimerExtension, // this activity could potentially be reused with a custom TimerExtension. TimerExtension timerExtension = context.GetExtension <TimerExtension>(); // The bookmark that will be resumed when the timer expires Bookmark bookmark = context.CreateBookmark(new BookmarkCallback(OnTimerExpired)); Console.WriteLine("AbsoluteDelay: Registering timer to expire at {0}", expTime.ToString()); timerExtension.RegisterTimer(delay, bookmark); } else { Console.WriteLine("AbsoluteDelay: Requested expiry time is already past due, skipping timer registration"); } }
public static TimerExtension GetTimerExtension(this ActivityContext context) { TimerExtension timerExtension = context.GetExtension <TimerExtension>(); if (timerExtension == null) { throw new ValidationException(nameof(TimerExtension) + " is not found."); } return(timerExtension); }
// The TimerExtensions support cancellation, so if this activity is canceled, propagate that signal to the TimerExtension protected override void Cancel(NativeActivityContext context) { base.Cancel(context); TimerExtension timerExtension = context.GetExtension <TimerExtension>(); Bookmark bookmark = this.timerExpiredBookmark.Get(context); if (bookmark != null) { timerExtension.CancelTimer(bookmark); } }