Example #1
0
        /// <summary>
        /// This method will try to fetch all live event handlers,
        /// and invokes each handler with the arguments.
        /// If a non-null <see cref="SynchronizationContext"/> has been set, it is checked
        /// by each handler.
        /// </summary>
        /// <param name="sender">NOT tested here.</param>
        /// <param name="eventArgs">NOT tested here.</param>
        /// <param name="sendOrPostPolicy">Invoke selection. Defaults to
        /// <see cref="SendOrPostPolicy.InvokeSafePostSafeOrInvokeUnknown"/>.</param>
        /// <returns>False if NO handlers are alive and the event is NOT invoked.
        /// True if the live handlers are all completed when this returns.
        /// Null if any live handler is Posted to the <see cref="SynchronizationContext"/>.</returns>
        public bool?TryInvoke(
            object sender,
            TEventArgs eventArgs,
            SendOrPostPolicy sendOrPostPolicy = SendOrPostPolicy.InvokeSafePostSafeOrInvokeUnknown)
        {
            bool?result = false;

            foreach (WeakEventDelegate <TEventArgs> weakEventDelegate in enumerateLiveDelegates())
            {
                switch (weakEventDelegate.TryInvoke(sender, eventArgs, sendOrPostPolicy))
                {
                case null:
                    result = null;
                    break;

                case true:
                    if (result == false)
                    {
                        result = true;
                    }
                    break;
                }
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// This method will try to fetch the event handler now and invoke the handler with the arguments.
        /// If a non-null <see cref="SynchronizationContext"/> has been set, it is checked,
        /// and this will Post, Send, or Invoke the handler, checking the current
        /// <see cref="SynchronizationContext"/> and the <see cref="SendOrPostPolicy"/>.
        /// </summary>
        /// <param name="sender">NOT tested here.</param>
        /// <param name="eventArgs">NOT tested here.</param>
        /// <param name="sendOrPostPolicy">Invoke selection. Defaults to
        /// <see cref="SendOrPostPolicy.InvokeSafePostSafeOrInvokeUnknown"/>.</param>
        /// <returns>False if the object is NOT alive and the handler is not invoked.
        /// True if the handler is completed when this returns.
        /// Null if the handler is Posted to the <see cref="SynchronizationContext"/>.</returns>
        public bool?TryInvoke(
            object sender,
            TEventArgs eventArgs,
            SendOrPostPolicy sendOrPostPolicy = SendOrPostPolicy.InvokeSafePostSafeOrInvokeUnknown)
        {
            if (!TryGetTarget(out EventHandler <TEventArgs> eventHandler))
            {
                return(false);
            }
            Task result = SynchronizationContext.SendOrPost(
                SendOrPostCallback,
                (eventHandler, sender, eventArgs),
                sendOrPostPolicy);

            void SendOrPostCallback((EventHandler <TEventArgs> handler, object source, TEventArgs args) state)
            => OnInvokeHandler(state.handler, state.source, state.args);

            return(result.IsCompleted
                                        ? true
                                        : (bool?)null);
        }