static async Task PostWithDeferralCoreAsync( this ISynchronizationContext context, Action action, uint deferralDepth, string memberName, string filePath, int lineNumber) { if (deferralDepth < 1) { await context.PostAsync(action, memberName, filePath, lineNumber); return; } await context.PostAsync( async() => { await PostWithDeferralCoreAsync( context, action, deferralDepth - 1, memberName, filePath, lineNumber); }); }
/// <summary> /// Invokes the specified action after the specified delay in milliseconds. /// Note: Be sure to wrap the action code in a try catch, /// as exceptions raised by the action are not handled. /// </summary> /// <param name="context"> /// The context on which the action will be invoked.</param> /// <param name="action"> /// The action to execute once the delay expires.</param> /// <param name="delayMs"> /// The time, in milleseconds, to wait before executing the action.</param> public static async Task PostWithDelayAsync( this ISynchronizationContext context, Action action, int delayMs, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0) { AssertArg.IsNotNull(context, nameof(context)); AssertArg.IsNotNull(action, nameof(action)); AssertArg.IsGreaterThan(-1, delayMs, nameof(delayMs)); if (delayMs >= 0) { await Task.Delay(delayMs); } await context.PostAsync( action, memberName, filePath, lineNumber); }