private void WorkerProc(object o) { const int waitTimeout = 10; WaitHandle[] waitHandles = {}; StreamingSourceVoice[] itemsCopy = {}; while (!_shutDown) { lock (_lockObject) { if (_itemsChanged) { itemsCopy = _items.ToArray(); waitHandles = itemsCopy.Select(x => x.BufferEndWaitHandle).Cast <WaitHandle>().ToArray(); _itemsChanged = false; } } if (waitHandles.Length == 0) { Thread.Sleep(waitTimeout); continue; } int index = WaitHandle.WaitAny(waitHandles, waitTimeout); if (index == WaitHandle.WaitTimeout) { continue; } StreamingSourceVoice item = itemsCopy[index]; //todo: make sure that we've got the right item item.Refill(); } }
/// <summary> /// Removes a <see cref="StreamingSourceVoice" /> from the <see cref="StreamingSourceVoiceListener" />. /// </summary> /// <param name="streamingSourceVoice"> /// The <see cref="StreamingSourceVoice" /> instance to remove from the /// <see cref="StreamingSourceVoiceListener" />. /// </param> public void Remove(StreamingSourceVoice streamingSourceVoice) { if (_items.Contains(streamingSourceVoice)) { lock (_lockObject) { _items.Remove(streamingSourceVoice); _itemsChanged = true; } } }
/// <summary> /// Adds a <see cref="StreamingSourceVoice" /> to the <see cref="StreamingSourceVoiceListener" />. /// </summary> /// <param name="streamingSourceVoice"> /// The <see cref="StreamingSourceVoice" /> instance to add to the /// <see cref="StreamingSourceVoiceListener" />. /// </param> public void Add(StreamingSourceVoice streamingSourceVoice) { if (!_items.Contains(streamingSourceVoice)) { if (_items.Count > MaxItems) //64 = Max waithandles { throw new NotSupportedException("The maximum number of items is limited to 64."); } lock (_lockObject) { _items.Add(streamingSourceVoice); _itemsChanged = true; TryStart(); } } }