@@ -289,25 +289,64 @@ internal Task StartAsync(int blockReadSize, int blockWriteSize, CancellationToke
289289 if ( ! acquisition . IsCompleted )
290290 throw new InvalidOperationException ( "Acquisition already running in the current context." ) ;
291291
292- // NB: Configure context before starting acquisition
292+
293+ // NB: Configure context before starting acquisition or the the settings (e.g. Block read
294+ // and write sizes) will not be respected
293295 var contextConfiguration = ConfigureContext ( ) ;
294- ctx . BlockReadSize = blockReadSize ;
295- ctx . BlockWriteSize = blockWriteSize ;
296-
297- // TODO: Stuff related to sync mode is 100% ONIX, not ONI. Therefore, in the long term,
298- // another place to do this separation might be needed
299- int address = ctx . HardwareAddress ;
300- int mode = ( address & 0x00FF0000 ) >> 16 ;
301- if ( mode == 0 ) // Standalone mode
296+
297+ try
298+ {
299+ // set block read and write size
300+ ctx . BlockReadSize = blockReadSize ;
301+ ctx . BlockWriteSize = blockWriteSize ;
302+
303+ // TODO: Stuff related to sync mode is 100% ONIX, not ONI. Therefore, in the long term,
304+ // another place to do this separation might be needed
305+ int address = ctx . HardwareAddress ;
306+ int mode = ( address & 0x00FF0000 ) >> 16 ;
307+ if ( mode == 0 ) // Standalone mode
308+ {
309+ ctx . Start ( true ) ;
310+ }
311+ else // If synchronized mode, reset counter independently
312+ {
313+ ctx . ResetFrameClock ( ) ;
314+ ctx . Start ( false ) ;
315+ }
316+
317+ }
318+ catch ( oni . ONIException ex ) when ( ex . Number == - 20 )
319+ {
320+ lock ( regLock )
321+ {
322+ ctx . Stop ( ) ;
323+ contextConfiguration . Dispose ( ) ;
324+ }
325+ throw new InvalidOperationException ( $ "The requested read size of { blockReadSize } bytes is too small for the current " +
326+ $ "hardware configuration, which requires at least { ctx . MaxReadFrameSize } bytes.", ex ) ;
327+ }
328+ catch ( oni . ONIException ex ) when ( ex . Number == - 24 )
302329 {
303- ctx . Start ( true ) ;
330+ lock ( regLock )
331+ {
332+ ctx . Stop ( ) ;
333+ contextConfiguration . Dispose ( ) ;
334+ }
335+ throw new InvalidOperationException ( $ "The requested write size of { blockWriteSize } bytes is too small for the current " +
336+ $ "hardware configuration, which requires at least { ctx . MaxWriteFrameSize } bytes.", ex ) ;
304337 }
305- else // If synchronized mode, reset counter independently
338+ catch
306339 {
307- ctx . ResetFrameClock ( ) ;
308- ctx . Start ( false ) ;
340+ lock ( regLock )
341+ {
342+ ctx . Stop ( ) ;
343+ contextConfiguration . Dispose ( ) ;
344+ }
345+ throw ;
309346 }
310347
348+ // TODO: If during the creation of of collectFramesCancellation, collectFramesToken, frameQueue, readFrames, or distributeFrames
349+ // an exception is thrown, contextConfiguration will not be disposed. The process will need to be restarted to get out of deadlock
311350 collectFramesCancellation = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken ) ;
312351 var collectFramesToken = collectFramesCancellation . Token ;
313352 var frameQueue = new BlockingCollection < oni . Frame > ( MaxQueuedFrames ) ;
0 commit comments