Skip to content

Commit e2cca03

Browse files
REQREPLY-221: Add timeout to request reply cs example
1 parent df9ed6b commit e2cca03

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

examples/connext_dds/request_reply/cs/PrimesProgram.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public static async Task Main(string[] args)
6464

6565
// Set up signal handler to Dispose the DDS entities
6666
var cancellationSource = new CancellationTokenSource();
67+
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(arguments.Timeout), cancellationSource.Token)
68+
.ContinueWith(_ =>
69+
{
70+
if (!cancellationSource.IsCancellationRequested)
71+
{
72+
Console.WriteLine("Shutting down due to timeout...");
73+
cancellationSource.Cancel();
74+
}
75+
}, TaskScheduler.Default);
76+
6777
Console.CancelKeyPress += (_, eventArgs) =>
6878
{
6979
Console.WriteLine("Shutting down...");
@@ -100,6 +110,7 @@ private class Arguments
100110
public int Domain { get; set; }
101111
public int N { get; set; } = int.MaxValue;
102112
public int PrimesPerReply { get; set; }
113+
public int Timeout { get; set; }
103114
}
104115

105116
// Uses the System.CommandLine package to parse the program arguments.
@@ -124,7 +135,11 @@ private static Arguments ParseArguments(string[] args)
124135
description: "The number to calculate primes up to (only applicable with --requester)"),
125136
new System.CommandLine.Option<int>(
126137
new string[] { "--primes-per-reply", "-p" },
127-
getDefaultValue: () => 5)
138+
getDefaultValue: () => 5),
139+
new System.CommandLine.Option<int>(
140+
new string[] { "--timeout" },
141+
getDefaultValue: () => 120,
142+
description: "Timeout in seconds to wait for the application to finish")
128143
};
129144

130145
rootCommand.Description = "Example RTI Connext Requester and Replier";

0 commit comments

Comments
 (0)