Skip to content

Commit

Permalink
OperationCanceledException by user code should not be handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Jan 14, 2020
1 parent c9bd9c8 commit 8b8af7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
22 changes: 20 additions & 2 deletions sandbox/SingleContainedApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SingleContainedApp
Expand Down Expand Up @@ -137,19 +138,36 @@ public class Person
public string Name { get; set; }
}


public class ThrowOperationCanceledException : ConsoleAppBase
{
public async Task Throw()
{
//while (true)
//{
// await Task.Delay(10);
// Context.CancellationToken.ThrowIfCancellationRequested();
//}

var cts = new CancellationTokenSource();
cts.Cancel();
cts.Token.ThrowIfCancellationRequested();
}
}

class Program
{
static async Task Main(string[] args)
{
args = new[] { "-array", "10,20,30", "-person", @"{""Age"":10,""Name"":""foo""}" };
//args = new[] { "-array", "10,20,30", "-person", @"{""Age"":10,""Name"":""foo""}" };


await Host.CreateDefaultBuilder()
.ConfigureLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Trace).ReplaceToSimpleConsole();
})
.RunConsoleAppFrameworkAsync<ComplexArgTest>(args);
.RunConsoleAppFrameworkAsync<ThrowOperationCanceledException>(args);
// .RunConsoleAppEngineAsync
//.ConfigureServices((hostContext, services) =>
//{
Expand Down
6 changes: 4 additions & 2 deletions src/ConsoleAppFramework/ConsoleAppEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ async Task RunCore(ConsoleAppContext ctx, Type type, MethodInfo methodInfo, stri
}
catch (Exception ex)
{
if (ex is OperationCanceledException || ex is TaskCanceledException)
if (ex is OperationCanceledException operationCanceledException && operationCanceledException.CancellationToken == cancellationToken)
{
return; // do nothing
// NOTE: Do nothing if the exception has thrown by the CancellationToken of ConsoleAppEngine.
// If the user code throws OperationCanceledException, ConsoleAppEngine should not handle that.
return;
}

if (ex is TargetInvocationException tex)
Expand Down

0 comments on commit 8b8af7c

Please sign in to comment.