-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0307eba
commit c12ec83
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using ROS2; | ||
|
||
namespace ConsoleApplication | ||
{ | ||
public static class RCLDotnetGuardCondition | ||
{ | ||
static GuardCondition _guardCondition = null; | ||
|
||
public static void Main(string[] args) | ||
{ | ||
RCLdotnet.Init(); | ||
|
||
var node = RCLdotnet.CreateNode("guard_condition_example"); | ||
|
||
_guardCondition = node.CreateGuardCondition(GuardConditionTriggered); | ||
|
||
Console.WriteLine($"RMWIdentifier: {RCLdotnet.GetRMWIdentifier()}"); | ||
|
||
_guardCondition.Trigger(); | ||
Console.WriteLine($"{DateTimeOffset.Now:O} ThreadId: {Environment.CurrentManagedThreadId} - Initial trigger of GuardCondition."); | ||
|
||
RCLdotnet.Spin(node); | ||
} | ||
|
||
static void GuardConditionTriggered() | ||
{ | ||
Console.WriteLine($"{DateTimeOffset.Now:O} ThreadId: {Environment.CurrentManagedThreadId} - GuardCondition was triggered."); | ||
_ = TriggerAfterSomeTime(); | ||
} | ||
|
||
static async Task TriggerAfterSomeTime() | ||
{ | ||
// This is a multiple of the 500ms used in the loop in | ||
// RCLdotnet.Spin(). So the guard condition will be triggered nearly | ||
// at the same time as the wait times out. This is to test/reproduce | ||
// if the combination of rcldotnet and the rmw implementation have | ||
// an race-conditions in that case. | ||
await Task.Delay(TimeSpan.FromSeconds(1)); | ||
|
||
// This will be called in an background worker thread as console | ||
// applications don't have an SynchronizationContext by default. | ||
_guardCondition.Trigger(); | ||
Console.WriteLine($"{DateTimeOffset.Now:O} ThreadId: {Environment.CurrentManagedThreadId} - Triggered GuardCondition."); | ||
} | ||
} | ||
} |