Ejemplo n.º 1
0
        public IAsyncEventHandler CreateHandler(Func <TArg, Task> handler,
                                                [CallerFilePath] string callerFile = null, [CallerLineNumber] int line = 0)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            lock (Handlers)
            {
                RemoveHandler(handler);

                var result = new AsyncEventTaskHandler <TArg>
                {
                    Action = handler,
                    Event  = this,
                    Caller = Debugger.IsAttached ? $"{callerFile}:{line}" : string.Empty
                };

                Handlers.Add(result);
                return(result);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates an event handler which you can dispose of explicitly if required.
        /// </summary>
        public static IAsyncEventHandler CreateHandler <TEvent>(this TEvent @event, Func <Task> handler,
                                                                [CallerFilePath] string callerFile = null, [CallerLineNumber] int line = 0)
            where TEvent : AbstractAsyncEvent
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            lock (@event.Handlers)
            {
                @event.RemoveHandler(handler);

                var result = new AsyncEventTaskHandler
                {
                    Action = handler,
                    Event  = @event,
                    Caller = Debugger.IsAttached ? $"{callerFile}:{line}" : string.Empty
                };

                @event.Handlers.Add(result);
                return(result);
            }
        }