本文整理汇总了C#中System.Threading.CancellationToken.Cancel方法的典型用法代码示例。如果您正苦于以下问题:C# CancellationToken.Cancel方法的具体用法?C# CancellationToken.Cancel怎么用?C# CancellationToken.Cancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.CancellationToken
的用法示例。
在下文中一共展示了CancellationToken.Cancel方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TerminateConnection
/// <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
}
}
示例2: LogicalThreadOperation
/// <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();
}