/// <summary> /// 异步调用事件 /// </summary> /// <param name="handlers">事件处理器</param> /// <param name="session">会话</param> /// <param name="e">事件数据</param> private static async Task InvokeAsync(AsyncEventHandler <TEventArgs> handlers, Session session, TEventArgs e) { if (handlers != null) { await handlers.Invoke(session, e); } }
/// <summary> /// Invokes an event if any event handler is registered. If none is registered, nothing happens. /// </summary> /// <typeparam name="T">The type of the event args.</typeparam> /// <param name="handler">The handler.</param> /// <param name="args">The arguments.</param> public static ValueTask SafeInvokeAsync <T>(this AsyncEventHandler <T>?handler, T args) { if (handler is null) { return(ValueTask.CompletedTask); } return(handler.Invoke(args)); }
/// <summary> /// Invokes an asynchronous event. /// </summary> /// <typeparam name="TEventArgs">the type of the event parameters</typeparam> /// <param name="eventHandler">the asynchronous event handler</param> /// <param name="sender">the object that is firing the event</param> /// <param name="eventArgs">the event parameters</param> /// <returns>a task that represents the asynchronous operation</returns> public static Task InvokeAsync <TEventArgs>(this AsyncEventHandler <TEventArgs> eventHandler, object sender, TEventArgs eventArgs) => eventHandler?.Invoke(sender, eventArgs) ?? Task.CompletedTask;
/// <summary> /// Invokes an asynchronous event. /// </summary> /// <param name="eventHandler">the asynchronous event handler</param> /// <param name="sender">the object that is firing the event</param> /// <param name="eventArgs"> /// the event parameters (if <see langword="null"/><see cref="EventArgs.Empty"/> is used) /// </param> /// <returns>a task that represents the asynchronous operation</returns> public static Task InvokeAsync(this AsyncEventHandler eventHandler, object sender, EventArgs eventArgs = null) => eventHandler?.Invoke(sender, eventArgs ?? EventArgs.Empty) ?? Task.CompletedTask;