Esempio n. 1
0
        /// <summary>
        /// Obtains the ISharedDispatcherTimer instance from the dictionary for the given <paramref name="tickInterval"/> (createing one if needed) and returns it.
        /// </summary>
        private static ISharedDispatcherTimer GetSharedTimer(double rate, TimeSpan tickInterval)
        {
            ISharedDispatcherTimer sharedDispatcherTimer = sharedTimerDictionary.SafeTryGetValue(tickInterval);

            if (sharedDispatcherTimer == null)
            {
                sharedTimerDictionary[tickInterval] = (sharedDispatcherTimer = new SharedDispatcherTimer(rate, tickInterval));
            }

            return(sharedDispatcherTimer);
        }
Esempio n. 2
0
        /// <summary>
        /// Obtains the ISharedDispatcherTimer instance from the dictionary (createing one if needed),
        /// Adds any of the optional Tick event handler signatures (as supported by the ISharedDispatchTimer's TickNotificationList)
        /// and returns an IDisposable token that will release use of the timer and will remove any given Tick event handler signatures from the underlying TickNotificationList when the token is dipsosed.
        /// </summary>
        public static IDisposable GetAndStartSharedTimer(double rate, string clientName, BasicNotificationDelegate onTickNotifyDelegate = null, INotifyable notifyableTarget = null, System.Threading.EventWaitHandle eventWaitHandle = null)
        {
            ISharedDispatcherTimer sharedDispatcherTimer = GetSharedTimer(rate);

            if (onTickNotifyDelegate != null)
            {
                sharedDispatcherTimer.TickNotificationList.OnNotify += onTickNotifyDelegate;
            }

            if (notifyableTarget != null)
            {
                sharedDispatcherTimer.TickNotificationList.AddItem(notifyableTarget);
            }

            if (eventWaitHandle != null)
            {
                sharedDispatcherTimer.TickNotificationList.AddItem(eventWaitHandle);
            }

            IDisposable token = sharedDispatcherTimer.GetRunTimerToken(clientName);

            return(new ExplicitDisposeActionList()
                   .AddItems(() => Fcns.DisposeOfGivenObject(token),
                             () =>
            {
                if (onTickNotifyDelegate != null)
                {
                    sharedDispatcherTimer.TickNotificationList.OnNotify -= onTickNotifyDelegate;
                }

                if (notifyableTarget != null)
                {
                    sharedDispatcherTimer.TickNotificationList.RemoveItem(notifyableTarget);
                }

                if (eventWaitHandle != null)
                {
                    sharedDispatcherTimer.TickNotificationList.RemoveItem(eventWaitHandle);
                }
            }));
        }