Пример #1
0
        public static void CopyToAsync(
            this Stream source,
            Stream destination,
            BlockingBag <byte[]> buffersPool,
            Action <Stream, Stream> onComplete)
        {
            if (!buffersPool.TryTake(out var buffer))
            {
                throw new InvalidOperationException("Buffers pool adding is completed. Cannot take the buffer");
            }

            source.BeginRead(buffer, 0, buffer.Length, ReadCallback, null);

            void ReadCallback(IAsyncResult writeResult)
            {
                var readBytes = source.EndRead(writeResult);

                if (readBytes > 0)
                {
                    destination.BeginWrite(buffer, 0, readBytes, WriteCallback, null);
                    return;
                }

                buffersPool.Add(buffer);
                onComplete(source, destination);
            }

            void WriteCallback(IAsyncResult writeResult)
            {
                destination.EndWrite(writeResult);
                source.BeginRead(buffer, 0, buffer.Length, ReadCallback, null);
            }
        }
Пример #2
0
        public BackgroundThreadPool(int workersCount)
        {
            tasks             = new BlockingBag <ITask>(workersCount);
            waitHandlerByTask = new Dictionary <ITask, ManualResetEvent>();

            for (var i = 0; i < workersCount; i++)
            {
                var thread = new Thread(Execute)
                {
                    IsBackground = true
                };
                thread.Start();
            }
        }