protected virtual void Dispose(bool disposing) { if (!m_Disposed) { if (disposing) { Stop(); m_CancellationToken?.Dispose(); m_Current?.Dispose(); m_ProcessingTask.Dispose(); SoundMsgStart?.Dispose(); SoundMsgEnd?.Dispose(); foreach (var msg in SupportedMessages) { msg?.Dispose(); } m_MasteringVoice.Dispose(); m_Device.Dispose(); } m_Disposed = true; } }
public void Start() { lock (m_StartStopMonitor) { if (m_ProcessingTask != null && !m_ProcessingTask.IsCompleted) { m_ProcessingTask.Wait(); m_ProcessingTask.Dispose(); } m_CancellationToken?.Dispose(); m_CancellationToken = new CancellationTokenSource(); foreach (var msg in SupportedMessages) { msg.m_Device = m_Device; } m_ProcessingTask = Task.Factory.StartNew(() => { try { while (!m_CancellationToken.IsCancellationRequested) { bool playInOut = false; var msgToPlay = m_MessagesQueue.PeekOrWait(m_CancellationToken.Token); { if (msgToPlay != null) { m_Current = msgToPlay; if (!playInOut) { playInOut = msgToPlay.PlayInOut; } if (playInOut) { SoundMsgStart?.PlaySync(); } msgToPlay.Play(); m_Current = null; } else { continue; } } if (playInOut) { SoundMsgEnd?.PlaySync(); } } } catch (ThreadInterruptedException) { /*ignored*/ } catch (OperationCanceledException) { /*ignored*/ } catch (Exception) { if (!m_CancellationToken.IsCancellationRequested) { Start(); } } }, m_CancellationToken.Token); } }