using System; using System.Threading; using System.Collections.Generic; namespace AudioStreamer { public class AudioStreamer { Thread thread; bool exitRequested; List sources; public AudioStreamer() { this.sources = new List(); exitRequested = false; this.thread = null; } public void addAudioSource(AudioSource source){ this.sources.Add(source); } public void removeAudioSource(AudioSource source){ this.sources.Remove(source); } public void Start(){ Monitor.Enter(this); if (thread == null){ Console.WriteLine("AudioStreamer.Start():"); exitRequested = false; thread = new Thread(pusher); thread.Start(); } Monitor.Exit(this); } public void Stop(){ Monitor.Enter(this); exitRequested = true; Monitor.Wait(this, 1000); this.thread = null; Monitor.Exit(this); } private void pusher(){ Monitor.Enter(this); try { Console.WriteLine("pusher(): start"); while (!exitRequested){ Monitor.Exit(this); foreach (AudioSource source in this.sources){ source.Source(); } Monitor.Enter(this); } } catch (Exception e){ Console.WriteLine("pusher(): Exception: {0}", e); } Console.WriteLine("pusher(): stop"); Monitor.PulseAll(this); Monitor.Exit(this); } } }