コード例 #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 void RunTask(ITask task)
        {
            if (waitHandlerByTask.ContainsKey(task))
            {
                throw new ArgumentException("Task already running");
            }

            waitHandlerByTask[task] = new ManualResetEvent(false);
            tasks.Add(task);
        }