Skip to content

Commit

Permalink
Fix returning Now in scaled realtime mode and also fix cancellation (#33
Browse files Browse the repository at this point in the history
)
  • Loading branch information
abeham committed May 4, 2020
1 parent 828d314 commit 645c1bf
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/SimSharp/Core/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,12 @@ public class PseudoRealtimeSimulation : ThreadSafeSimulation {
/// The current model time. Note that, while in realtime, this may continuously change.
/// </summary>
public override DateTime Now {
get { lock (_timeLocker) { return base.Now + _rtDelayTime.Elapsed; } }
get {
lock (_timeLocker) {
if (!IsRunningInRealtime) return base.Now;
return base.Now + TimeSpan.FromMilliseconds(_rtDelayTime.Elapsed.TotalMilliseconds * RealtimeScale.Value);
}
}
protected set => base.Now = value;
}

Expand Down Expand Up @@ -1009,7 +1014,7 @@ public class PseudoRealtimeSimulation : ThreadSafeSimulation {
var observed = _rtDelayTime.Elapsed;

lock (_locker) {
if (rtScale.Value != 1.0) observed = TimeSpan.FromMilliseconds(observed.TotalMilliseconds / rtScale.Value);
if (rtScale.Value != 1.0) observed = TimeSpan.FromMilliseconds(observed.TotalMilliseconds * rtScale.Value);
if (_rtDelayCtrl.IsCancellationRequested && observed < delay) {
lock (_timeLocker) {
Now = base.Now + observed;
Expand Down

0 comments on commit 645c1bf

Please sign in to comment.