Exemplo n.º 1
0
        /// <summary>
        /// Runs looped formatted messages until the timer runs out
        /// </summary>
        /// <param name="shutdownDelay">Total amount of message time</param>
        /// <param name="shutdownAnnouncement">What to announce</param>
        /// <param name="shutdownAnnouncementFrequency">How often to announce it</param>
        private static async void RunAnnouncements(int shutdownDelay, string shutdownAnnouncement, int shutdownAnnouncementFrequency)
        {
            int secondsLeftBeforeShutdown = shutdownDelay;

            while (secondsLeftBeforeShutdown > 0)
            {
                SystemComm.BroadcastToAll(String.Format(shutdownAnnouncement, secondsLeftBeforeShutdown));
                await Task.Delay(shutdownAnnouncementFrequency * 1000);

                secondsLeftBeforeShutdown -= shutdownAnnouncementFrequency;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to shutdown an existing loop
        /// </summary>
        /// <param name="designator">what the loop is called</param>
        /// <param name="shutdownDelay">In # of Seconds - how long to wait before cancelling it</param>
        /// <param name="shutdownAnnouncementFrequency">In # of Seconds - how often to announce the shutdown, <= 0 is only one announcement</param>
        /// <param name="shutdownAnnouncement">What to announce (string.format format) before shutting down, empty string = no announcements, ex "World shutting down in {0} seconds."</param>
        public static void ShutdownLoop(string designator, int shutdownDelay, string shutdownAnnouncement, int shutdownAnnouncementFrequency = -1)
        {
            var cancelToken = GetCancellationToken(designator);

            if (cancelToken != null && cancelToken.Token.CanBeCanceled)
            {
                cancelToken.CancelAfter(shutdownDelay * 1000);
            }

            if (!string.IsNullOrWhiteSpace(shutdownAnnouncement))
            {
                SystemComm.BroadcastToAll(String.Format(shutdownAnnouncement, shutdownDelay));
            }

            if (shutdownAnnouncementFrequency > 0)
            {
                Task.Run(() => RunAnnouncements(shutdownDelay, shutdownAnnouncement, shutdownAnnouncementFrequency));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Cancel ALL the current loops
        /// </summary>
        /// <param name="shutdownDelay">In # of Seconds - how long to wait before cancelling it</param>
        /// <param name="shutdownAnnouncementFrequency">In # of Seconds - how often to announce the shutdown, < = 0 is only one announcement</param>
        /// <param name="shutdownAnnouncement">What to announce (string.format format) before shutting down, empty string = no announcements</param>
        public static void ShutdownAll(int shutdownDelay, string shutdownAnnouncement, int shutdownAnnouncementFrequency = -1)
        {
            IEnumerable <CancellationTokenSource> tokens
                = globalCache.Where(kvp => kvp.Value.GetType() == typeof(CancellationTokenSource)).Select(kvp => (CancellationTokenSource)kvp.Value);

            foreach (var token in tokens.Where(tk => !tk.IsCancellationRequested && tk.Token.CanBeCanceled))
            {
                if (token != null)
                {
                    token.CancelAfter(shutdownDelay * 1000);
                }
            }

            if (!string.IsNullOrWhiteSpace(shutdownAnnouncement))
            {
                SystemComm.BroadcastToAll(String.Format(shutdownAnnouncement, shutdownDelay));
            }

            if (shutdownAnnouncementFrequency > 0)
            {
                Task.Run(() => RunAnnouncements(shutdownDelay, shutdownAnnouncement, shutdownAnnouncementFrequency));
            }
        }