Esempio n. 1
0
        /// <summary>
        /// Completes the actions for terminating a session.
        /// </summary>
        protected virtual void CompleteTermination()
        {
            Trace.TraceInformation("ClientSession[{0}]: Executing terminate actions", this.SessionId);

            try
            {
                List <KeyValuePair <string, Action <bool> > > torun = null;
                this.actionsOnTerminateLock.EnterWriteLock();

                try
                {
                    if (this.actionsOnTerminate != null)
                    {
                        // we need a copy of the list because the actions can modify the collection itself
                        torun = new List <KeyValuePair <string, Action <bool> > >(this.actionsOnTerminate);

                        this.actionsOnTerminate = null;
                    }

                    this.State = SessionState.Terminating;
                }
                finally
                {
                    this.actionsOnTerminateLock.ExitWriteLock();
                }

                if (torun != null)
                {
                    Trace.TraceInformation("ClientSession[{0}]: Running terminate actions. count={1}", this.SessionId, torun.Count);
                    int watcherTerminateActionsCount = 0;

                    // Run the watcher terminate actions first.
                    foreach (KeyValuePair <string, Action <bool> > item in torun)
                    {
                        if (item.Key.StartsWith("watcher-"))
                        {
                            watcherTerminateActionsCount++;
                            item.Value(true);
                        }
                    }

                    ClientSession.RemoveAllBulkWatchers(this.SessionId);

                    // Then run the non watcher terminate actions.
                    foreach (KeyValuePair <string, Action <bool> > item in torun)
                    {
                        if (!item.Key.StartsWith("watcher-"))
                        {
                            item.Value(true);
                        }
                    }

                    Trace.TraceInformation("ClientSession[{0}]: Completed terminate actions. count={1}, watcherTerminateActionsCount={2}", this.SessionId, torun.Count, watcherTerminateActionsCount);
                }

                this.State = SessionState.Closed;
            }
            finally
            {
                if (this.OnTerminated != null)
                {
                    this.OnTerminated();
                }
            }

            Trace.WriteLine(string.Format("ClientSession[{0}]: Terminated", this.SessionId));
        }