예제 #1
0
        /// <summary>
        /// Subscribes a delegate to an event.
        /// </summary>
        /// <param name="action">The delegate that gets executed when the event is published.</param>
        /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param>
        /// <param name="keepSubscriberReferenceAlive">When <see langword="true"/>, the <see cref="PubSubEvent"/> keeps a reference to the subscriber so it does not get garbage collected.</param>
        /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns>
        /// <remarks>
        /// If <paramref name="keepSubscriberReferenceAlive"/> is set to <see langword="false" />, <see cref="PubSubEvent"/> will maintain a <see cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate.
        /// If not using a WeakReference (<paramref name="keepSubscriberReferenceAlive"/> is <see langword="true" />), the user must explicitly call Unsubscribe for the event when disposing the subscriber in order to avoid memory leaks or unexpected behavior.
        /// <para/>
        /// The PubSubEvent collection is thread-safe.
        /// </remarks>
        public SubscriptionToken Subscribe(Action action, ThreadOption threadOption, bool keepSubscriberReferenceAlive)
        {
            IDelegateReference actionReference = new DelegateReference(action, keepSubscriberReferenceAlive);

            EventSubscription subscription;
            switch (threadOption)
            {
                case ThreadOption.PublisherThread:
                    subscription = new EventSubscription(actionReference);
                    break;
                case ThreadOption.BackgroundThread:
                    subscription = new BackgroundEventSubscription(actionReference);
                    break;
                case ThreadOption.UIThread:
                    if (SynchronizationContext == null) throw new InvalidOperationException(Resources.EventAggregatorNotConstructedOnUIThread);
                    subscription = new DispatcherEventSubscription(actionReference, SynchronizationContext);
                    break;
                default:
                    subscription = new EventSubscription(actionReference);
                    break;
            }


            return base.InternalSubscribe(subscription);
        }
예제 #2
0
 public override SubscriptionToken Subscribe(Action<FundOrder> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<FundOrder> filter)
 {
     SubscribeArgumentAction = action;
     SubscribeArgumentFilter = filter;
     ThreadOption = threadOption;
     SubscribeCount++;
     return new SubscriptionToken(t=> { });
 }
예제 #3
0
		protected EventSubscription(EventBase parentEvent,
									Action action,
									ThreadOption threadOption,
									ReferenceOption referenceOption)
			: base(parentEvent, threadOption, referenceOption)
		{
			Verify.ArgumentNotNull(action, "action");
			DelegateReference = new DelegateReference(action, referenceOption);
		}
		public UIThreadSubscription(EventBase parentEvent, Action action, ThreadOption threadOption,
		                            ReferenceOption referenceOption)
			: base(parentEvent, action, threadOption, referenceOption)
		{
			if (ThreadOption != ThreadOption.UIThread && ThreadOption != ThreadOption.UIThreadPost)
			{
				throw new InvalidOperationException("Incorrect thread option");
			}
		}
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Subscription"/> class
        /// </summary>
        internal Subscription(
            WorkItemSubscriptions workItemSubscriptions,
            object subscriber,
            string handlerMethodName,
            Type[] parameterTypes,
            ThreadOption threadOption)
        {
            this.wrSubscriber = new WeakReference(subscriber);
            this.handlerMethodName = handlerMethodName;
            this.threadOption = threadOption;
            this.workItemSubscriptions = workItemSubscriptions;

            MethodInfo miHandler = GetMethodInfo(subscriber, handlerMethodName, parameterTypes);
            if (miHandler == null)
            {
                throw new EventBrokerException(String.Format(CultureInfo.CurrentCulture,
                    Properties.Resources.SubscriberHandlerNotFound,
                    handlerMethodName, subscriber.GetType().ToString()));
            }
            else if (miHandler.IsStatic)
            {
                throw new EventBrokerException(String.Format(CultureInfo.CurrentCulture,
                    Properties.Resources.CannotRegisterStaticSubscriptionMethods,
                    miHandler.DeclaringType.FullName, miHandler.Name));

            }

            this.typeHandle = subscriber.GetType().TypeHandle;
            this.methodHandle = miHandler.MethodHandle;
            ParameterInfo[] parameters = miHandler.GetParameters();
            if (IsValidEventHandler(parameters))
            {
                ParameterInfo pInfo = miHandler.GetParameters()[1];
                Type pType = pInfo.ParameterType;
                handlerEventArgsType = typeof(EventHandler<>).MakeGenericType(pType);
            }
            else
            {
                throw new EventBrokerException(String.Format(CultureInfo.CurrentCulture,
                    Properties.Resources.InvalidSubscriptionSignature,
                    miHandler.DeclaringType.FullName, miHandler.Name));
            }

            if (threadOption == ThreadOption.UserInterface)
            {
                // If there's a syncronization context (i.e. the WindowsFormsSynchronizationContext
                // created to marshal back to the thread where a control was initially created
                // in a particular thread), capture it to marshal back to it through the
                // context, that basically goes through a Post/Send.
                if (SynchronizationContext.Current != null)
                {
                    syncContext = SynchronizationContext.Current;
                }
            }
        }
예제 #6
0
		/// <summary>
		/// Subscribe to the event.
		/// </summary>
		/// <param name="action">Action to take when the event is published.</param>
		/// <param name="threadOption">Specifies which thread the action will be performed on.</param>
		/// <param name="referenceOption">Specifies whether the event subscription will hold a strong or 
		/// weak reference on the action delegate.</param>
		/// <returns>Returns an <see cref="IEventSubscription"/> object that represents the subscription.</returns>
		public IEventSubscription Subscribe(Action action, ThreadOption threadOption,
											ReferenceOption referenceOption)
		{
			Verify.ArgumentNotNull(action, "action");
			EventSubscription eventSubscription = CreateEventSubscription(action, threadOption, referenceOption);
			lock (_eventSubscriptions)
			{
				_eventSubscriptions.Add(eventSubscription);
			}
			return eventSubscription;
		}
		public BackgroundThreadSubscription(EventBase parentEvent,
											Action action,
											ThreadOption threadOption,
											ReferenceOption referenceOption)
			: base(parentEvent, action, threadOption, referenceOption)
		{
			if (ThreadOption != ThreadOption.BackgroundThread)
			{
				throw new InvalidOperationException("Invalid thread option");
			}
		}
		protected EventSubscriptionBase(EventBase parentEvent, ThreadOption threadOption, ReferenceOption referenceOption)
		{
			Verify.ArgumentNotNull(parentEvent, "parentEvent", out _event);
			IsSubscribed = true;
			ThreadOption = threadOption;
		}
예제 #9
0
 /// <summary>
 /// Subscribes a delegate to an event.
 /// PubSubEvent will maintain a <see cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate.
 /// </summary>
 /// <param name="action">The delegate that gets executed when the event is raised.</param>
 /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param>
 /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns>
 /// <remarks>
 /// The PubSubEvent collection is thread-safe.
 /// </remarks>
 public SubscriptionToken Subscribe(Action action, ThreadOption threadOption)
 {
     return(Subscribe(action, threadOption, false));
 }
예제 #10
0
 /// <summary>
 /// Adds a subcription to this <see cref="EventTopic"/>.
 /// </summary>
 /// <param name="subscriber">The object that contains the method that will handle the <see cref="EventTopic"/>.</param>
 /// <param name="handlerMethodName">The name of the method on the subscriber that will handle the <see cref="EventTopic"/>.</param>
 /// <param name="workItem">The <see cref="WorkItem"/> where the subscriber is.</param>
 /// <param name="threadOption">A <see cref="ThreadOption"/> value indicating how the handler method should be called.</param>
 public virtual void AddSubscription(object subscriber, string handlerMethodName, WorkItem workItem, ThreadOption threadOption)
 {
     AddSubscription(subscriber, handlerMethodName, null, workItem, threadOption);
 }
예제 #11
0
 public WeakChannelSubscription(ThreadOption threadOption, IObserver <T> subscriber)
     : base(threadOption)
 {
     Guard.ArgumentNotNull(subscriber, "subscriber");
     _subscriber = new WeakReference(subscriber);
 }
예제 #12
0
 public void AddSubscription(object subscriber, string handlerMethodName, ThreadOption threadOption)
 {
     AddSubscription(subscriber, handlerMethodName, null, threadOption);
 }
예제 #13
0
 /// <summary>
 /// Subscribes a delegate to an event.
 /// </summary>
 /// <param name="action">The delegate that gets executed when the event is published.</param>
 /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param>
 /// <param name="keepSubscriberReferenceAlive">When <see langword="true"/>, the <seealso cref="CompositeWpfEvent{TPayload}"/> keeps a reference to the subscriber so it does not get garbage collected.</param>
 /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns>
 /// <remarks>
 /// If <paramref name="keepSubscriberReferenceAlive"/> is set to <see langword="false" />, <see cref="CompositeWpfEvent{TPayload}"/> will maintain a <seealso cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate.
 /// If not using a WeakReference (<paramref name="keepSubscriberReferenceAlive"/> is <see langword="true" />), the user must explicitly call Unsubscribe for the event when disposing the subscriber in order to avoid memory leaks or unexepcted behavior.
 ///
 /// The CompositeWpfEvent collection is thread-safe.
 /// </remarks>
 public SubscriptionToken Subscribe(Action <TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive)
 {
     return(Subscribe(action, threadOption, keepSubscriberReferenceAlive, delegate { return true; }));
 }
 public void ShowThreadingOption(List <ThreadOption> threadingOptions, ThreadOption selected)
 {
     _threadingOptions = threadingOptions;
     _threadingOption  = selected;
 }
예제 #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WeakSubscription"/> class.
 /// </summary>
 /// <param name="subject">The subject.</param>
 /// <param name="handlerAction">The handler action.</param>
 /// <param name="dispatcher">The UI thread dispatcher.</param>
 /// <param name="threadOption">The thread option.</param>
 internal WeakSubscription(EventSubject subject, Delegate handlerAction, Dispatcher dispatcher, ThreadOption threadOption)
     : this(subject, CheckHandlerAction(handlerAction).Target, CheckHandlerAction(handlerAction).Method, dispatcher, threadOption)
 {
 }
예제 #16
0
파일: EventBroker.cs 프로젝트: codeno47/efc
        /// <summary>
        /// Registers the subscriber.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethodName">Name of the handler method.</param>
        /// <param name="threadOption">The thread option.</param>
        public void RegisterSubscriber(string subject, object subscriber, string handlerMethodName, ThreadOption threadOption)
        {
            Requires.NotNull(subject, "subject");
            Requires.NotNull(subscriber, "subscriber");
            Requires.NotNullOrEmpty(handlerMethodName, "handlerMethodName");

            this.VerifyThreadOption(threadOption);

            lock (this.subjects)
            {
                EventSubject publishedEvent = this.GetEvent(subject);
                publishedEvent.AddSubscription(new WeakSubscription(publishedEvent, subscriber, handlerMethodName, this.dispatcher, threadOption));
            }
        }
예제 #17
0
파일: EventBroker.cs 프로젝트: codeno47/efc
        /// <summary>
        /// Registers the subscriber.
        /// </summary>
        /// <typeparam name="T">Type of the event arguments.</typeparam>
        /// <param name="subject">The subject.</param>
        /// <param name="handlerAction">The handler action.</param>
        /// <param name="threadOption">The thread option.</param>
        public void RegisterSubscriber <T>(string subject, EventHandler <T> handlerAction, ThreadOption threadOption) where T : EventArgs
        {
            Requires.NotNull(subject, "subject");
            Requires.NotNull(handlerAction, "handlerAction");

            this.VerifyThreadOption(threadOption);

            this.AddSubscribtionFor(subject, handlerAction, threadOption);
        }
예제 #18
0
 public HandlesAttribute(Type eventType, ThreadOption threadOption, bool keepSubscriberReferenceAlive)
 {
     EventType    = eventType;
     ThreadOption = ThreadOption;
     KeepSubscriberReferenceAlive = keepSubscriberReferenceAlive;
 }
예제 #19
0
 public HandlesAttribute(Type eventType, ThreadOption threadOption)
 {
     EventType    = eventType;
     ThreadOption = threadOption;
     KeepSubscriberReferenceAlive = true;
 }
예제 #20
0
 public void Subscribe(Action <TPayload> action, ThreadOption threadOption)
 {
     Subscribe(action, threadOption, false);
 }
예제 #21
0
		/// <summary>
		/// Subscribe to the event.
		/// </summary>
		/// <param name="action">Action to take when the event is published.</param>
		/// <param name="threadOption">Specifies which thread the action will be performed on.</param>
		/// <returns>Returns an <see cref="IEventSubscription"/> object that represents the subscription.</returns>
		public IEventSubscription Subscribe(Action action, ThreadOption threadOption)
		{
			return Subscribe(action, threadOption, ReferenceOption.WeakReference);
		}
예제 #22
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WeakSubscription"/> class.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethodInfo">The handler method info.</param>
        /// <param name="dispatcher">The UI thread dispatcher.</param>
        /// <param name="threadOption">The thread option.</param>
        protected WeakSubscription(EventSubject subject, object subscriber, MethodInfo handlerMethodInfo, Dispatcher dispatcher, ThreadOption threadOption)
            : base(handlerMethodInfo, dispatcher, threadOption)
        {
            Requires.NotNull(subject, "subject");
            Requires.NotNull(subscriber, "subscriber");
            Requires.NotNull(handlerMethodInfo, "handlerMethodInfo");

            if (handlerMethodInfo.IsStatic)
            {
                throw ExceptionBuilder.ArgumentNotValid("handlerMethodInfo", Messages.PassedMethodCantBeStatic);
            }

            this._handlerMethodName  = handlerMethodInfo.Name;
            this.subject             = subject;
            this.subscriberReference = new WeakReference(subscriber);
            this.typeHandle          = subscriber.GetType().TypeHandle;
            this.methodHandle        = handlerMethodInfo.MethodHandle;
        }
예제 #23
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ThreadedMessageAction" /> class.
 /// </summary>
 /// <param name="action">The action.</param>
 /// <param name="threadOption">The thread option.</param>
 public ThreadedMessageAction(Action <TMessage> action, ThreadOption threadOption = ThreadOption.Current)
 {
     Action       = action;
     ThreadOption = threadOption;
 }
예제 #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WeakSubscription"/> class.
 /// </summary>
 /// <param name="subject">The subject.</param>
 /// <param name="subscriber">The subscriber.</param>
 /// <param name="handlerMethodName">Name of the handler method.</param>
 /// <param name="dispatcher">The UI thread dispatcher.</param>
 /// <param name="threadOption">The thread option.</param>
 internal WeakSubscription(EventSubject subject, object subscriber, string handlerMethodName, Dispatcher dispatcher, ThreadOption threadOption)
     : this(subject, subscriber, GetMethodInfo(subscriber, handlerMethodName, null), dispatcher, threadOption)
 {
 }
예제 #25
0
 /// <summary>
 ///     Method to subscribe for a filter event
 /// </summary>
 /// <typeparam name="TEvent"></typeparam>
 /// <param name="action"></param>
 /// <param name="threadOption"></param>
 /// <param name="keepSubscriberReferenceAlive"></param>
 /// <param name="filter"></param>
 /// <returns></returns>
 public static SubscriptionToken Subscribe <TEvent>(Action <TEvent> action,
                                                    ThreadOption threadOption = ThreadOption.PublisherThread, bool keepSubscriberReferenceAlive = false,
                                                    Predicate <TEvent> filter = null)
 {
     return(GetEvent <TEvent>().Subscribe(action, threadOption, keepSubscriberReferenceAlive, filter));
 }
예제 #26
0
 public override SubscriptionToken Subscribe(Action <IDictionary <string, decimal> > action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate <IDictionary <string, decimal> > filter)
 {
     SubscribeArgumentAction       = action;
     SubscribeArgumentFilter       = filter;
     SubscribeArgumentThreadOption = threadOption;
     return(null);
 }
예제 #27
0
 public override SubscriptionToken Subscribe(Action <PositionPayloadEventArgs> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate <PositionPayloadEventArgs> filter)
 {
     this.SubscribeArgumentAction       = action;
     this.SubscribeArgumentFilter       = filter;
     this.SubscribeArgumentThreadOption = threadOption;
     return(null);
 }
예제 #28
0
        public static SubscriptionToken SubscribeAsync <TPayload>(this PubSubEvent <TPayload> pubSubEvent, Func <TPayload, Task> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive)
        {
            if (pubSubEvent == null)
            {
                throw new ArgumentNullException(nameof(pubSubEvent));
            }

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            return(pubSubEvent.Subscribe(async(payload) =>
            {
                try
                {
                    await action(payload);
                }
                catch (Exception exp)
                {
                    BitExceptionHandlerBase.Current?.OnExceptionReceived(exp);
                }
            }, threadOption: threadOption, keepSubscriberReferenceAlive: keepSubscriberReferenceAlive));
        }
예제 #29
0
        /// <summary>
        /// Adds a subcription to this <see cref="EventTopic"/>.
        /// </summary>
        /// <param name="subscriber">The object that contains the method that will handle the <see cref="EventTopic"/>.</param>
        /// <param name="handlerMethodName">The name of the method on the subscriber that will handle the <see cref="EventTopic"/>.</param>
        /// <param name="workItem">The <see cref="WorkItem"/> where the subscriber is.</param>
        /// <param name="threadOption">A <see cref="ThreadOption"/> value indicating how the handler method should be called.</param>
        /// <param name="parameterTypes">Defines the types and order of the parameters for the subscriber. For none pass null.
        /// Use this overload when there are several methods with the same name on the subscriber.</param>
        public virtual void AddSubscription(object subscriber, string handlerMethodName, Type[] parameterTypes, WorkItem workItem, ThreadOption threadOption)
        {
            Guard.ArgumentNotNull(subscriber, "subscriber");
            Guard.ArgumentNotNullOrEmptyString(handlerMethodName, "handlerMethodName");
            Guard.EnumValueIsDefined(typeof(ThreadOption), threadOption, "threadOption");
            Clean();
            WorkItemSubscriptions wis = FindWorkItemSubscription(workItem);
            if (wis == null)
            {
                wis = new WorkItemSubscriptions(workItem);
                workItemSubscriptions.Add(wis);
            }
            wis.AddSubscription(subscriber, handlerMethodName, parameterTypes, threadOption);

            if (traceSource != null)
                traceSource.TraceInformation(Properties.Resources.EventTopicTraceSubscriptionAdded,
                    name, handlerMethodName, subscriber.GetType().ToString());
        }
        } // Subscribe()

        /// <summary>
        /// Subscribes a delegate to an event.
        /// PubSubEvent will maintain a <see cref="WeakReference"/> to the Target of 
        /// the supplied <paramref name="action"/> delegate.
        /// </summary>
        /// <param name="action">The delegate that gets executed when the event is 
        /// raised.</param>
        /// <param name="threadOption">Specifies on which thread to receive the 
        /// delegate callback.</param>
        /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the 
        /// added subscription.</returns>
        /// <remarks>
        /// The PubSubEvent collection is thread-safe.
        /// </remarks>
        public SubscriptionToken Subscribe(Action<TPayload> action, 
            ThreadOption threadOption)
        {
            return this.Subscribe(action, threadOption, false);
        } // Subscribe()
예제 #31
0
 public override SubscriptionToken Subscribe(Action <IUserService> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate <IUserService> filter)
 {
     SubscribeArgumentAction       = action;
     SubscribeArgumentFilter       = filter;
     SubscribeArgumentThreadOption = threadOption;
     return(null);
 }
예제 #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Subscription"/> class.
 /// </summary>
 internal Subscription(
     WorkItemSubscriptions workItemSubscriptions, object subscriber, string handlerMethodName, ThreadOption threadOption)
     : this(workItemSubscriptions, subscriber, handlerMethodName, null, threadOption)
 {
 }
예제 #33
0
 /// <summary>
 /// Subscribes a delegate to an event.
 /// PubSubEvent will maintain a <see cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate.
 /// </summary>
 /// <param name="action">The delegate that gets executed when the event is raised.</param>
 /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param>
 /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns>
 /// <remarks>
 /// The PubSubEvent collection is thread-safe.
 /// </remarks>
 public SubscriptionToken Subscribe(Action action, ThreadOption threadOption) => Subscribe(action, threadOption, false);
예제 #34
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Subscription"/> class.
 /// </summary>
 internal Subscription(
     WorkItemSubscriptions workItemSubscriptions, object subscriber, string handlerMethodName, ThreadOption threadOption)
     : this(workItemSubscriptions, subscriber, handlerMethodName, null, threadOption)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StateChangedAttribute"/> using the provided
 /// topic and thread options.
 /// </summary>
 /// <param name="topic">The state topic.</param>
 /// <param name="option">The threading option.</param>
 public StateChangedAttribute(string topic, ThreadOption option)
     : base(StateChangedTopic.BuildStateChangedTopicString(topic))
 {
     this.Thread = option;
 }
예제 #36
0
 public void AddSubscription(object subscriber, string handlerMethodName, Type[] parameterTypes, ThreadOption threadOption)
 {
     lock (lockObject)
     {
         if (FindSubscription(subscriber, handlerMethodName) == null)
         {
             subscriptions.Add(new Subscription(this, subscriber, handlerMethodName, parameterTypes, threadOption));
         }
     }
 }
예제 #37
0
		/// <summary>
		/// Create the right type of event subscription for the thread option
		/// </summary>
		private EventSubscription CreateEventSubscription(Action action, ThreadOption threadOption,
																	ReferenceOption referenceOption)
		{
			switch (threadOption)
			{
				case ThreadOption.PublisherThread:
					return new PublishThreadSubscription(this, action, threadOption, referenceOption);
				case ThreadOption.UIThread:
				case ThreadOption.UIThreadPost:
					return new UIThreadSubscription(this, action, threadOption, referenceOption);
				case ThreadOption.BackgroundThread:
					return new BackgroundThreadSubscription(this, action, threadOption, referenceOption);
			}

			// this should not happen.
			throw new ArgumentException("Unknown thread option: " + threadOption);
		}
예제 #38
0
 public override SubscriptionToken Subscribe(Action<string> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<string> filter)
 {
     SubscribeArgumentAction = action;
     SubscribeArgumentFilter = filter;
     return null;
 }
예제 #39
0
 /// <summary>
 /// Đăng ký một đơn vị xử lý sự kiện
 /// </summary>
 /// <param name="subscriber">Đơn vị xử lí sự kiện được đăng ký</param>
 /// <param name="threadOption">Tùy chọn Thread ở đó thực hiện thao tác đăng ký</param>
 public void Subscribe(Action <T> subscriber, ThreadOption threadOption)
 {
     InternalSubscribe(subscriber, threadOption);
 }
예제 #40
0
 /// <summary>
 /// Subscribes a delegate to an event.
 /// </summary>
 /// <param name="action">The delegate that gets executed when the event is published.</param>
 /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param>
 /// <param name="keepSubscriberReferenceAlive">When <see langword="true"/>, the <seealso cref="CompositeWpfEvent{TPayload}"/> keeps a reference to the subscriber so it does not get garbage collected.</param>
 /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns>
 /// <remarks>
 /// If <paramref name="keepSubscriberReferenceAlive"/> is set to <see langword="false" />, <see cref="CompositeWpfEvent{TPayload}"/> will maintain a <seealso cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate.
 /// If not using a WeakReference (<paramref name="keepSubscriberReferenceAlive"/> is <see langword="true" />), the user must explicitly call Unsubscribe for the event when disposing the subscriber in order to avoid memory leaks or unexepcted behavior.
 ///
 /// The CompositeWpfEvent collection is thread-safe.
 /// </remarks>
 public SubscriptionToken Subscribe(Action <TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive)
 {
     return(Subscribe(action, threadOption, keepSubscriberReferenceAlive, DefaultFilter));
 }
 /// <summary>
 /// Delcares a method as an <see cref="EventTopic"/> subscription using the specified <see cref="ThreadOption"/>.
 /// </summary>
 /// <param name="topic">The name of the <see cref="EventTopic"/> to subscribe to.</param>
 /// <param name="threadOption">The <see cref="ThreadOption"/> indicating how the method should be called.</param>
 public EventSubscriptionAttribute(string topic, ThreadOption threadOption)
 {
     this.topic = topic;
     this.threadOption = threadOption;
 }
 public static SubscriptionToken SubscribeAsync <TPayload>(this PubSubEvent <TPayload> pubSubEvent, Func <TPayload, Task> action, ThreadOption threadOption)
 {
     return(pubSubEvent.Subscribe(async(payload) =>
     {
         try
         {
             await action(payload).ConfigureAwait(false);
         }
         catch (Exception exp)
         {
             BitExceptionHandler.Current.OnExceptionReceived(exp);
         }
     }, threadOption: threadOption));
 }
 public static SubscriptionToken SubscribeAsync <TPayload>(this PubSubEvent <TPayload> pubSubEvent, Func <TPayload, Task> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate <TPayload> filter)
 {
     return(pubSubEvent.Subscribe(async(payload) =>
     {
         try
         {
             await action(payload).ConfigureAwait(false);
         }
         catch (Exception exp)
         {
             BitExceptionHandler.Current.OnExceptionReceived(exp);
         }
     }, threadOption: threadOption, keepSubscriberReferenceAlive: keepSubscriberReferenceAlive, filter: filter));
 }
예제 #44
0
 public SubscriptionOptionsAttribute(ThreadOption option, SubscriptionScope scope = SubscriptionScope.EventType)
 {
     Option = option;
 }
예제 #45
0
파일: PubSubEvent.cs 프로젝트: dersia/Prism
 /// <summary>
 /// Subscribes a delegate to an event.
 /// PubSubEvent will maintain a <see cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate.
 /// </summary>
 /// <param name="action">The delegate that gets executed when the event is raised.</param>
 /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param>
 /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns>
 /// <remarks>
 /// The PubSubEvent collection is thread-safe.
 /// </remarks>
 public SubscriptionToken Subscribe(Action action, ThreadOption threadOption)
 {
     return Subscribe(action, threadOption, false);
 }
예제 #46
0
 /// <summary>
 ///     Method to subscribe for an event
 /// </summary>
 /// <typeparam name="TEvent"></typeparam>
 /// <param name="action"></param>
 /// <param name="threadOption"></param>
 /// <param name="keepSubscriberReferenceAlive"></param>
 /// <returns></returns>
 public static SubscriptionToken Subscribe <TEvent>(Action action,
                                                    ThreadOption threadOption = ThreadOption.PublisherThread, bool keepSubscriberReferenceAlive = false)
 {
     return(Subscribe <TEvent>(e => action(), threadOption, keepSubscriberReferenceAlive));
 }
예제 #47
0
 public override SubscriptionToken Subscribe(Action<IDictionary<string, decimal>> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<IDictionary<string, decimal>> filter)
 {
     SubscribeArgumentAction = action;
     SubscribeArgumentFilter = filter;
     SubscribeArgumentThreadOption = threadOption;
     return null;
 }
예제 #48
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SubscriptionBase"/> class.
        /// </summary>
        /// <param name="handlerMethodInfo">The handler method info.</param>
        /// <param name="dispatcher">The UI thread dispatcher.</param>
        /// <param name="threadOption">The thread option.</param>
        protected SubscriptionBase(MethodInfo handlerMethodInfo, Dispatcher dispatcher, ThreadOption threadOption)
        {
            Requires.NotNull(handlerMethodInfo, "handlerMethodInfo");
            if (!handlerMethodInfo.IsEventHandler())
            {
                throw ExceptionBuilder.ArgumentNotValid("handlerMethodInfo", Messages.InvalidEventHandlerParameters);
            }

            this.ThreadOption = threadOption;
            ParameterInfo paramInfo = handlerMethodInfo.GetParameters()[1];
            Type          paramType = paramInfo.ParameterType;

            this.handlerEventArgsType = paramType;

            this.dispatcher = dispatcher;
        }