Changes from Code Analysis. Implemented WaitHandle for Async operation. Not used or tested.

This commit is contained in:
Mark Hamann 2016-02-26 10:32:23 -08:00
parent 0659c119e3
commit 4c40f7ebcc
2 changed files with 104 additions and 3 deletions

View file

@ -27,7 +27,7 @@ namespace MorseTrainer
/// <summary>
/// WaveStream holds a memory stream with a WAV waveform
/// </summary>
public class WaveStream
public class WaveStream : IDisposable
{
/// <summary>
/// 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
}
}

View file

@ -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
/// </summary>
public class BuildWaverformAsync : IAsyncResult
public class BuildWaverformAsync : IAsyncResult, IDisposable
{
/// <summary>
/// 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
}
}