From 98ec7683366520870b44277653ddb32dc79c792b Mon Sep 17 00:00:00 2001 From: jonnew Date: Wed, 12 Feb 2025 12:11:40 -0500 Subject: [PATCH 1/2] Tie lifetime of oni.Context to that of ContextTask - Instead of disposing and recreating oni.Context when a reset occurs, issue a oni.Context.Refresh() to existing object --- OpenEphys.Onix1/ContextTask.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/OpenEphys.Onix1/ContextTask.cs b/OpenEphys.Onix1/ContextTask.cs index b8c8dc8a..0081c3b8 100644 --- a/OpenEphys.Onix1/ContextTask.cs +++ b/OpenEphys.Onix1/ContextTask.cs @@ -93,12 +93,12 @@ internal ContextTask(string driver, int index) groupedFrames.Connect(); contextDriver = driver; contextIndex = index; + ctx = new oni.Context(contextDriver, contextIndex); Initialize(); } private void Initialize() { - ctx = new oni.Context(contextDriver, contextIndex); SystemClockHz = ctx.SystemClockHz; AcquisitionClockHz = ctx.AcquisitionClockHz; MaxReadFrameSize = ctx.MaxReadFrameSize; @@ -115,8 +115,11 @@ internal void Reset() lock (readLock) lock (writeLock) { - ctx?.Dispose(); - Initialize(); + if (ctx != null) + { + ctx.Refresh(); + Initialize(); + } } } } @@ -196,7 +199,6 @@ internal void Reset() return groupedFrames.Where(deviceFrames => deviceFrames.Key == deviceAddress).Merge(); } - void AssertConfigurationContext() { if (disposed) From 2145257754e96a19b2b95f0ac99e901933f785c4 Mon Sep 17 00:00:00 2001 From: jonnew Date: Thu, 13 Feb 2025 11:46:59 -0500 Subject: [PATCH 2/2] Address review comments - Make ContextTask.ctx readonly per @glopesdev's comments - Bump package version --- Directory.Build.props | 2 +- OpenEphys.Onix1/ContextTask.cs | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 6e2f4293..6f9706e0 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -13,7 +13,7 @@ LICENSE true icon.png - 0.4.3 + 0.4.4 10.0 strict diff --git a/OpenEphys.Onix1/ContextTask.cs b/OpenEphys.Onix1/ContextTask.cs index 0081c3b8..dfa01001 100644 --- a/OpenEphys.Onix1/ContextTask.cs +++ b/OpenEphys.Onix1/ContextTask.cs @@ -35,7 +35,7 @@ namespace OpenEphys.Onix1 /// public class ContextTask : IDisposable { - oni.Context ctx; + readonly oni.Context ctx; /// /// Maximum amount of frames the reading queue will hold. If the queue fills or the read thread is not @@ -115,11 +115,8 @@ internal void Reset() lock (readLock) lock (writeLock) { - if (ctx != null) - { - ctx.Refresh(); - Initialize(); - } + ctx.Refresh(); + Initialize(); } } } @@ -289,8 +286,9 @@ internal Task StartAsync(int blockReadSize, int blockWriteSize, CancellationToke } if (!acquisition.IsCompleted) + { throw new InvalidOperationException("Acquisition already running in the current context."); - + } // NB: Configure context before starting acquisition or the the settings (e.g. Block read // and write sizes) will not be respected @@ -486,7 +484,9 @@ internal void EnsureContext(Action action) lock (disposeLock) { if (!disposed) + { action(); + } } } @@ -562,8 +562,7 @@ private void DisposeContext() lock (readLock) lock (writeLock) { - ctx?.Dispose(); - ctx = null; + ctx.Dispose(); } }