-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (84 loc) · 2.31 KB
/
Copy pathProgram.cs
File metadata and controls
100 lines (84 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using KrahmerSoft.ParallelFileCopierLib;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace KrahmerSoft.ParallelFileCopierCli
{
internal class Program
{
private static ParallelFileCopierOptionsCli _optionsCli;
private static CancellationTokenSource _cancellationTokenSource;
private static bool _sigintReceived;
private static int Main(string[] args)
{
return MainAsync(args).GetAwaiter().GetResult();
}
private static async Task<int> MainAsync(string[] args)
{
_optionsCli = new ParallelFileCopierOptionsCli();
CommandLineParser.ParseArguments(args, _optionsCli);
if (!_optionsCli.ValidateOptions())
return 1;
_cancellationTokenSource = new CancellationTokenSource();
ListenForCancelation();
using (var parallelFileCopier = new ParallelFileCopier(_optionsCli))
{
parallelFileCopier.VerboseOutput += HandleVerboseOutput;
try
{
await parallelFileCopier.CopyFilesAsync(_optionsCli.SourcePath, _optionsCli.DestinationPath, _cancellationTokenSource.Token);
if (_cancellationTokenSource.IsCancellationRequested)
return 1;
return 0;
}
catch (OperationCanceledException)
{
return 1;
}
catch (ApplicationException ex)
{
Console.Error.WriteLine(ex.Message);
return 1;
}
catch (ArgumentException ex)
{
Console.Error.WriteLine(ex.Message);
return 1;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
return 1;
}
finally
{
parallelFileCopier.VerboseOutput -= HandleVerboseOutput;
}
}
}
private static void ListenForCancelation()
{
Console.CancelKeyPress += (a, e) =>
{
_sigintReceived = true;
// Tell .NET to not terminate the process
e.Cancel = true;
Console.WriteLine("Received SIGINT (Ctrl+C) - Gracefully canceling...");
_cancellationTokenSource.Cancel();
};
AppDomain.CurrentDomain.ProcessExit += (a, b) =>
{
if (!_sigintReceived)
return; // ignore - normal termination
Console.WriteLine("Received SIGTERM - Gracefully canceling...");
_cancellationTokenSource.Cancel();
};
}
private static void HandleVerboseOutput(object sender, VerboseInfo e)
{
if (e.VerboseLevel > _optionsCli.ShowVerboseLevel)
return;
Console.WriteLine($"{e.Message}");
}
}
}