public void EnqueueForWriting(ByteBlock _block) { int id = _block.ID; lock (locker) { if (isDead) { throw new InvalidOperationException("Queue already stopped"); } while (id != blockId) { Monitor.Wait(locker); } queue.Enqueue(_block); blockId++; Monitor.PulseAll(locker); } }
private void Compress(object i) { try { ManualResetEvent doneEvent = doneEvents[(int)i]; while (!_cancelled) { ByteBlock _block = _queueReader.Dequeue(); if (_block == null) { doneEvent.Set(); return; } using (MemoryStream memoryStream = new MemoryStream()) { using (GZipStream cs = new GZipStream(memoryStream, CompressionMode.Compress)) { cs.Write(_block.Buffer, 0, _block.Buffer.Length); } byte[] compressedData = memoryStream.ToArray(); ByteBlock _out = new ByteBlock(_block.ID, compressedData); _queueWriter.EnqueueForWriting(_out); } } doneEvent.Set(); } catch (Exception ex) { Console.WriteLine("Error in thread number {0}. \n Error description: {1}", i, ex.Message); _cancelled = true; } }