public void SetException (Exception exception)
{
- if (!task.TrySetException (new AggregateException (exception)))
- throw new InvalidOperationException ("The task has already completed");
+ if (exception is OperationCanceledException) {
+ if (Task.TrySetCanceled ())
+ return;
+ } else {
+ if (Task.TrySetException (new AggregateException (exception)))
+ return;
+ }
+
+ throw new InvalidOperationException ("The task has already completed");
}
public void SetStateMachine (IAsyncStateMachine stateMachine)
public void SetException (Exception exception)
{
- if (!task.TrySetException (new AggregateException (exception)))
- throw new InvalidOperationException ("The task has already completed");
+ if (exception is OperationCanceledException) {
+ if (Task.TrySetCanceled ())
+ return;
+ } else {
+ if (Task.TrySetException (new AggregateException (exception)))
+ return;
+ }
+
+ throw new InvalidOperationException ("The task has already completed");
}
public void SetStateMachine (IAsyncStateMachine stateMachine)
--- /dev/null
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+class Program
+{
+ static async Task<int> TestCanceled()
+ {
+ await Task.FromResult(1);
+ throw new OperationCanceledException();
+ }
+
+ static async Task TestCanceled_2()
+ {
+ await Task.FromResult(1);
+ throw new OperationCanceledException();
+ }
+
+ static async Task<int> TestException()
+ {
+ await Task.FromResult(1);
+ throw new ApplicationException();
+ }
+
+ static int Main()
+ {
+ bool canceled = false;
+ var t = TestCanceled().ContinueWith(l =>
+ {
+ canceled = l.IsCanceled;
+ }, TaskContinuationOptions.ExecuteSynchronously);
+
+ t.Wait();
+
+ if (!canceled)
+ return 1;
+
+ if (t.Exception != null)
+ return 2;
+
+ t = TestCanceled_2().ContinueWith(l =>
+ {
+ canceled = l.IsCanceled;
+ }, TaskContinuationOptions.ExecuteSynchronously);
+
+ t.Wait();
+
+ if (!canceled)
+ return 11;
+
+ if (t.Exception != null)
+ return 12;
+
+ bool faulted = false;
+ t = TestException().ContinueWith(l =>
+ {
+ faulted = l.IsFaulted;
+ }, TaskContinuationOptions.ExecuteSynchronously);
+
+ if (!faulted)
+ return 21;
+
+ if (t.Exception != null)
+ return 22;
+
+ Console.WriteLine("ok");
+ return 0;
+ }
+}
</method>
</type>
</test>
+ <test name="test-async-32.cs">
+ <type name="Program">
+ <method name="System.Threading.Tasks.Task`1[System.Int32] TestCanceled()" attrs="145">
+ <size>33</size>
+ </method>
+ <method name="System.Threading.Tasks.Task TestCanceled_2()" attrs="145">
+ <size>33</size>
+ </method>
+ <method name="System.Threading.Tasks.Task`1[System.Int32] TestException()" attrs="145">
+ <size>33</size>
+ </method>
+ <method name="Int32 Main()" attrs="145">
+ <size>248</size>
+ </method>
+ <method name="Void .ctor()" attrs="6278">
+ <size>7</size>
+ </method>
+ </type>
+ <type name="Program+<TestCanceled>c__async0">
+ <method name="Void MoveNext()" attrs="486">
+ <size>160</size>
+ </method>
+ <method name="Void SetStateMachine(IAsyncStateMachine)" attrs="486">
+ <size>13</size>
+ </method>
+ </type>
+ <type name="Program+<TestCanceled_2>c__async1">
+ <method name="Void MoveNext()" attrs="486">
+ <size>159</size>
+ </method>
+ <method name="Void SetStateMachine(IAsyncStateMachine)" attrs="486">
+ <size>13</size>
+ </method>
+ </type>
+ <type name="Program+<TestException>c__async2">
+ <method name="Void MoveNext()" attrs="486">
+ <size>160</size>
+ </method>
+ <method name="Void SetStateMachine(IAsyncStateMachine)" attrs="486">
+ <size>13</size>
+ </method>
+ </type>
+ <type name="Program+<Main>c__AnonStorey3">
+ <method name="Void <>m__0(System.Threading.Tasks.Task`1[System.Int32])" attrs="131">
+ <size>14</size>
+ </method>
+ <method name="Void <>m__1(System.Threading.Tasks.Task)" attrs="131">
+ <size>14</size>
+ </method>
+ <method name="Void <>m__2(System.Threading.Tasks.Task`1[System.Int32])" attrs="131">
+ <size>14</size>
+ </method>
+ <method name="Void .ctor()" attrs="6278">
+ <size>7</size>
+ </method>
+ </type>
+ </test>
<test name="test-cls-00.cs">
<type name="CLSCLass_6">
<method name="Void add_Disposed(Delegate)" attrs="2182">