本文整理汇总了C#中CancellationToken.ThrowIfCancellationRequested方法的典型用法代码示例。如果您正苦于以下问题:C# CancellationToken.ThrowIfCancellationRequested方法的具体用法?C# CancellationToken.ThrowIfCancellationRequested怎么用?C# CancellationToken.ThrowIfCancellationRequested使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CancellationToken
的用法示例。
在下文中一共展示了CancellationToken.ThrowIfCancellationRequested方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run1
static void Run1(CancellationToken ct)
{
ct.ThrowIfCancellationRequested();
Console.WriteLine("我是任务1");
Thread.Sleep(2000);
ct.ThrowIfCancellationRequested();
Console.WriteLine("我是任务1的第二部分信息");
}
示例2: Sum
private static Int32 Sum(CancellationToken cancelToken, Int32 n) {
Int32 sum = 0;
for (; n > 0; n--){
cancelToken.ThrowIfCancellationRequested();
checked{ sum += n; }
}
return sum;
}
示例3: Sum
private static Int32 Sum(CancellationToken ct, Int32 n){
Int32 sum = 0;
for (; n > 0; n--) {
// The following line throws OperationCanceledException when Cancel
// is called on the CancellationTokenSource referred to by the token
ct.ThrowIfCancellationRequested();
checked { sum += n; }// if n is large, this will throw System.OverflowException
}
return sum;
}
示例4: Sum
private static int Sum(CancellationToken ct, int n)
{
int sum = 0;
for (; n > 0; n--)
{
ct.ThrowIfCancellationRequested();
checked { sum += n; }
}
return sum;
}
示例5: AsyncOperation1
void AsyncOperation1 (CancellationToken cancellationToken, object state){
Console.WriteLine("Starting AsyncOperation Thread");
int count = (int)state;
for(int i=0; i<count; i++)
{
if(cancellationToken.IsCancellationRequested){
Console.WriteLine("Cancelling the operation");
cancellationToken.ThrowIfCancellationRequested();
}else{
Console.WriteLine(i);
}
}
}
示例6: Wait
public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken) {
if (millisecondsTimeout < -1)
throw new ArgumentOutOfRangeException("millisecondsTimeout");
ThrowIfDisposed();
if (!IsSet) {
var wait = new SpinWait();
while (!IsSet) {
if (wait.Count < spinCount) {
wait.SpinOnce();
continue;
}
break;
}
cancellationToken.ThrowIfCancellationRequested();
if (IsSet)
return true;
WaitHandle handle = WaitHandle;
if (cancellationToken.CanBeCanceled) {
int result = WaitHandle.WaitAny(new[] {handle, cancellationToken.WaitHandle}, millisecondsTimeout, false);
if (result == 1)
throw new OperationCanceledException(cancellationToken.ToString());
if (result == WaitHandle.WaitTimeout)
return false;
} else {
if (!handle.WaitOne(millisecondsTimeout, false))
return false;
}
}
return true;
}
示例7: Sum
private static Int32 Sum(CancellationToken ct, Int32 n) {
Int32 sum = 0;
for (; n > 0; n--) {
// The following line throws OperationCanceledException when Cancel
// is called on the CancellationTokenSource referred to by the token
ct.ThrowIfCancellationRequested();
//Thread.Sleep(0); // Simulate taking a long time
checked { sum += n; }
}
return sum;
}
示例8: Wait
public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
{
bool result;
cancellationToken.Register(Cancel);
if (millisecondsTimeout != Timeout.Infinite)
{
result = Event.Await(millisecondsTimeout, TimeUnit.MILLISECONDS);
}
else
{
Event.Await();
result = true;
}
cancellationToken.ThrowIfCancellationRequested();
return result;
}
示例9: Wait
public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
{
if (millisecondsTimeout < -1)
throw new ArgumentOutOfRangeException ("millisecondsTimeout");
ThrowIfDisposed ();
if (!set) {
SpinWait wait = new SpinWait ();
while (!set) {
cancellationToken.ThrowIfCancellationRequested ();
if (wait.Count < spinCount) {
wait.SpinOnce ();
continue;
}
break;
}
if (set)
return true;
WaitHandle handle = WaitHandle;
if (cancellationToken.CanBeCanceled) {
if (WaitHandle.WaitAny (new[] { handle, cancellationToken.WaitHandle }, millisecondsTimeout, false) == 0)
return false;
cancellationToken.ThrowIfCancellationRequested ();
} else {
if (!handle.WaitOne (millisecondsTimeout, false))
return false;
}
}
return true;
}
示例10: ComputeTaskWithAcknowledgement
public static void ComputeTaskWithAcknowledgement(CancellationToken token)
{
for (int i=0; i<100000000; i++)
token.ThrowIfCancellationRequested();
}
示例11: strassen_mult_parallel
//.........这里部分代码省略.........
matrix_sub(n_2, n_2,
A, ax + n_2, ay, a_s,
A, ax, ay, a_s,
g_cum, 0, 0, n_2);
matrix_add(n_2, n_2,
B, bx, by, bs,
B, bx, by + n_2, bs,
h_cum, 0, 0, n_2);
strassen_mult_parallel(
cancellationToken,
n_2,
g_cum, 0, 0, n_2,
h_cum, 0, 0, n_2,
p6, 0, 0, n_2,
s);
}, cancellationToken);
// p7 = (a12 - a22) x (b21 + b22)
Task t_p7 = Task.Factory.StartNew(() =>
{
matrix_sub(n_2, n_2,
A, ax, ay + n_2, a_s,
A, ax + n_2, ay + n_2, a_s,
i_cum, 0, 0, n_2);
matrix_add(n_2, n_2,
B, bx + n_2, by, bs,
B, bx + n_2, by + n_2, bs,
j_cum, 0, 0, n_2);
strassen_mult_parallel(
cancellationToken,
n_2,
i_cum, 0, 0, n_2,
j_cum, 0, 0, n_2,
p7, 0, 0, n_2,
s);
}, cancellationToken);
try { Task.WaitAll(t_p1, t_p2, t_p3, t_p4, t_p5, t_p6, t_p7); }
catch (AggregateException ae)
{
ae.Flatten().Handle(e => e is TaskCanceledException);
cancellationToken.ThrowIfCancellationRequested();
}
// c11 = p1 + p4 - p5 + p7
Task t_c11 = Task.Factory.StartNew(() =>
{
matrix_add(n_2, n_2,
p1, 0, 0, n_2,
p4, 0, 0, n_2,
C, cx, cy, cs);
matrix_sub(n_2, n_2,
C, cx, cy, cs,
p5, 0, 0, n_2,
C, cx, cy, cs);
matrix_add(n_2, n_2,
C, cx, cy, cs,
p7, 0, 0, n_2,
C, cx, cy, cs);
});
// c12 = p3 + p5
Task t_c12 = Task.Factory.StartNew(() =>
{
matrix_add(n_2, n_2,
p3, 0, 0, n_2,
p5, 0, 0, n_2,
C, cx, cy + n_2, cs);
});
// c21 = p2 + p4
Task t_c21 = Task.Factory.StartNew(() =>
{
matrix_add(n_2, n_2,
p2, 0, 0, n_2,
p4, 0, 0, n_2,
C, cx + n_2, cy, cs);
});
// c22 = p1 + p3 - p2 + p6
Task t_c22 = Task.Factory.StartNew(() =>
{
matrix_add(n_2, n_2,
p1, 0, 0, n_2,
p3, 0, 0, n_2,
C, cx + n_2, cy + n_2, cs);
matrix_sub(n_2, n_2,
C, cx + n_2, cy + n_2, cs,
p2, 0, 0, n_2,
C, cx + n_2, cy + n_2, cs);
matrix_add(n_2, n_2,
C, cx + n_2, cy + n_2, cs,
p6, 0, 0, n_2,
C, cx + n_2, cy + n_2, cs);
});
Task.WaitAll(t_c11, t_c12, t_c21, t_c22);
}
}
}
示例12: matrix_mult_serial
private static void matrix_mult_serial(
CancellationToken cancellationToken,
// dimensions of A (lxm), B(mxn), and C(lxn) submatrices
int l, int m, int n,
// (ax,ay) = origin of A submatrix for multiplicand
int* A, int ax, int ay, int a_s,
// (bx,by) = origin of B submatrix for multiplicand
int* B, int bx, int by, int bs,
// (cx,cy) = origin of C submatrix for result
int* C, int cx, int cy, int cs)
{
for (int i = 0; i < l; ++i)
{
cancellationToken.ThrowIfCancellationRequested();
for (int j = 0; j < n; j++)
{
int temp = 0;
for (int k = 0; k < m; k++)
{
temp += A[(i + ax) * a_s + k + ay] * B[(k + bx) * bs + j + by];
}
C[(i + cx) * cs + j + cy] = temp;
}
}
}
示例13: Wait
public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
{
if (millisecondsTimeout < -1)
throw new ArgumentOutOfRangeException ("millisecondsTimeout",
"millisecondsTimeout is a negative number other than -1");
long start = millisecondsTimeout == -1 ? 0 : sw.ElapsedMilliseconds;
SpinWait wait = new SpinWait ();
while (state == isNotSet) {
cancellationToken.ThrowIfCancellationRequested ();
if (millisecondsTimeout > -1 && (sw.ElapsedMilliseconds - start) > millisecondsTimeout)
return false;
if (wait.Count < spinCount) {
wait.SpinOnce ();
} else {
int waitTime = millisecondsTimeout == -1 ? -1 : Math.Max (millisecondsTimeout - (int)(sw.ElapsedMilliseconds - start) , 1);
WaitHandle handle = WaitHandle;
if (state == isSet)
return true;
if (WaitHandle.WaitAny (new[] { handle, cancellationToken.WaitHandle }, waitTime, false) == 0)
return true;
}
}
return true;
}
示例14: Wait
public bool Wait(int millisTimeout, CancellationToken ctk) {
ctk.ThrowIfCancellationRequested();
if (millisTimeout < Timeout.Infinite)
throw new ArgumentOutOfRangeException("millisTimeout");
if (IsSet) {
return true;
}
if (millisTimeout == 0)
return false;
int expiresTickCount = 0;
bool timedOut = false;
int timeout = millisTimeout;
if (millisTimeout != Timeout.Infinite) {
expiresTickCount = Environment.TickCount + millisTimeout;
timedOut = true;
}
int defaultSpin = DEFAULT_SPIN_MP; // 10
int sleep0Modulus = DEFAULT_SPIN_MP / 2;
int sleep1Modulus = DEFAULT_SPIN_MP * 2;
int spinCount = SpinCount;
for (int i = 0; i < spinCount; i++) {
if (IsSet)
return true;
if (i < defaultSpin) {
if (i == (defaultSpin / 2)) {
Thread.Yield();
} else {
Thread.SpinWait(Environment.ProcessorCount * (((int) 4) << i));
}
} else if ((i % sleep1Modulus) == 0) {
Thread.Sleep(1);
} else if ((i % sleep0Modulus) == 0) {
Thread.Sleep(0);
} else {
Thread.Yield();
}
//
// Check if the wait was cancelled.
//
if (i >= 100 && (i % 10) == 0) {
ctk.ThrowIfCancellationRequested();
}
}
EnsureLockObjectCreated();
using (ctk.Register(s_cancellationTokenCallback, this)) {
lock (m_lock) {
int entryStateVersion = stateVersion;
do {
if (IsSet || entryStateVersion != stateVersion) {
return true;
}
ctk.ThrowIfCancellationRequested();
//
// Adjust timeout, if any
//
if (timedOut) {
int now = Environment.TickCount;
if (expiresTickCount <= now)
return false;
timeout = expiresTickCount - now;
}
//
// Increment waiters, and check IsSet again
//
Waiters++;
if (IsSet) {
Waiters--;
return true;
}
try {
if (!Monitor.Wait(m_lock, timeout)) {
return false;
}
} finally {
Waiters--;
}
} while (true);
}
}
}
示例15: ReadAsync
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return Read(buffer, offset, count);
}