/// <summary> /// Processes the termination of client. /// </summary> private void TerminateConnection(CancellationToken cancellationToken) { try { // Cancel all asynchronous loops associated with the cancellation token and notify user // of terminated connection if the connection had not previously been terminated if (!cancellationToken.Cancel()) OnConnectionTerminated(); } catch (ThreadAbortException) { // This is a normal exception throw; } catch { // Other exceptions can happen (e.g., NullReferenceException) if thread // resumes and the class is disposed middle way through this method } }
/// <summary> /// Creates a new instance of the <see cref="LogicalThreadOperation"/> class. /// </summary> /// <param name="thread">The thread on which to execute the operation's action.</param> /// <param name="action">The action to be executed.</param> /// <param name="priority">The priority with which the action should be executed on the logical thread.</param> /// <param name="autoRunIfPending"> /// Set to <c>true</c> to execute <see cref="RunIfPending"/> automatically; otherwise, /// set to <c>false</c> for user controlled call timing. /// </param> /// <exception cref="ArgumentException"><paramref name="priority"/> is outside the range between 1 and <see cref="LogicalThread.PriorityLevels"/>.</exception> public LogicalThreadOperation(LogicalThread thread, Action action, int priority, bool autoRunIfPending = true) { m_thread = thread; m_action = action; Priority = priority; m_autoRunIfPending = autoRunIfPending; // Initialize this class with a cancelled token so that // calls to EnsurePriority before the first call to // ExecuteActionAsync do not inadvertently queue actions m_cancellationToken = new CancellationToken(); m_cancellationToken.Cancel(); }