From 4c40f7ebccd058b5b3ad89535932d13e7d49fe75 Mon Sep 17 00:00:00 2001 From: Mark Hamann Date: Fri, 26 Feb 2016 10:32:23 -0800 Subject: [PATCH] Changes from Code Analysis. Implemented WaitHandle for Async operation. Not used or tested. --- MorseTrainer/WaveStream.cs | 43 ++++++++++++++++++++- MorseTrainer/WordToToneBuilder.cs | 64 ++++++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/MorseTrainer/WaveStream.cs b/MorseTrainer/WaveStream.cs index c90d29d..7cb4f3e 100644 --- a/MorseTrainer/WaveStream.cs +++ b/MorseTrainer/WaveStream.cs @@ -27,7 +27,7 @@ namespace MorseTrainer /// /// WaveStream holds a memory stream with a WAV waveform /// - public class WaveStream + public class WaveStream : IDisposable { /// /// Creates a WAV of the Morse code of 'text' @@ -196,5 +196,46 @@ namespace MorseTrainer private String _text; private System.IO.MemoryStream _stream; + + #region IDisposable Support + private bool disposedValue = false; // To detect redundant calls + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + // TODO: dispose managed state (managed objects). + if (_stream != null) + { + _stream.Close(); + _stream.Dispose(); + _stream = null; + } + } + + // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. + // TODO: set large fields to null. + + disposedValue = true; + } + } + + // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources. + // ~WaveStream() { + // // Do not change this code. Put cleanup code in Dispose(bool disposing) above. + // Dispose(false); + // } + + // This code added to correctly implement the disposable pattern. + public void Dispose() + { + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. + Dispose(true); + // TODO: uncomment the following line if the finalizer is overridden above. + // GC.SuppressFinalize(this); + } + #endregion } } diff --git a/MorseTrainer/WordToToneBuilder.cs b/MorseTrainer/WordToToneBuilder.cs index 07d79d2..f32e675 100644 --- a/MorseTrainer/WordToToneBuilder.cs +++ b/MorseTrainer/WordToToneBuilder.cs @@ -103,6 +103,7 @@ namespace MorseTrainer WaveStream stream = new WaveStream(buildInfo.Word, soundsList, ToneGenerator.SAMPLES_PER_SECOND, _toneGenerator.SamplesPerCycle); buildInfo.SetWaveform(stream); buildInfo.Callback(); + buildInfo.TriggerWaitHandle(); } private ToneGenerator _toneGenerator; @@ -112,7 +113,7 @@ namespace MorseTrainer /// A BuildWaverformAsync object implements the IAsyncResult interface for /// WordToToneBuilder.StartBuildAsync /// - public class BuildWaverformAsync : IAsyncResult + public class BuildWaverformAsync : IAsyncResult, IDisposable { /// /// Builds the IAsyncResult object for 'word' @@ -153,7 +154,25 @@ namespace MorseTrainer { get { - throw new NotImplementedException(); + lock(this) + { + if (_semaphore == null) + { + _semaphore = new Semaphore(1, 1); + } + } + return _semaphore; + } + } + + public void TriggerWaitHandle() + { + lock(this) + { + if (_semaphore != null) + { + _semaphore.Release(); + } } } @@ -204,5 +223,46 @@ namespace MorseTrainer private String _word; private AsyncCallback _callback; private WaveStream _waveStream; + private Semaphore _semaphore; + + #region IDisposable Support + private bool disposedValue = false; // To detect redundant calls + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + // TODO: dispose managed state (managed objects). + if (_semaphore != null) + { + _semaphore.Dispose(); + _semaphore = null; + } + } + + // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. + // TODO: set large fields to null. + + disposedValue = true; + } + } + + // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources. + // ~BuildWaverformAsync() { + // // Do not change this code. Put cleanup code in Dispose(bool disposing) above. + // Dispose(false); + // } + + // This code added to correctly implement the disposable pattern. + public void Dispose() + { + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. + Dispose(true); + // TODO: uncomment the following line if the finalizer is overridden above. + // GC.SuppressFinalize(this); + } + #endregion } }