Exemplo n.º 1
0
        public Pipeline()
        {
            PipelineBegin = StartBlock <string>();

            DownloadBlock = TransformBlock <string, byte[]>(
                async(job, request) =>
            {
                //var data = await HttpClient.GetByteArrayAsync(request);

                var data = " FJDKSLJF:KDSFJDSKLJF:KLDJSKJFLK:DSJJFKDSJF:JDSKJF:LKDSJLJF:DSKLJFL:KDSJLF:JDSL:JFDS";

                await Task.Delay(delay += 50);

                return(Encoding.ASCII.GetBytes(data));
            }, new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = 25
            });

            MergeBlock = MergeTransformBlock <byte[], byte[]>(
                (job, byteArrays) =>
            {
                var length = 0;

                foreach (var byteArray in byteArrays)
                {
                    length += byteArray.Length;
                }

                var mergedArray = new byte[length];
                var index       = 0;

                foreach (var byteArray in byteArrays)
                {
                    byteArray.CopyTo(mergedArray, index);

                    index += byteArray.Length;
                }

                return(mergedArray);
            }, new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = 5
            });

            DiskWriteBlock = TransformBlock <byte[], bool>(
                async(job, data) =>
            {
                return(await WriteToFile(job.FileName, data));
            }, new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = 1
            });

            ImageBlock = TransformBlock <byte[], bool>(
                async(job, data) =>
            {
                return(await WriteToFile(job.FileName + ".png", data));
            }, new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = 1
            });

            FinishBlock = ActionBlock <Tuple <bool, bool> >(
                (job, flags) =>
            {
                if (flags.Item1)
                {
                    Console.WriteLine("File successfull");
                }

                if (flags.Item2)
                {
                    Console.WriteLine("Image successfull");
                }
            }, true);

            PipelineBegin.LinkTo(DownloadBlock);

            DownloadBlock.LinkTo(MergeBlock, e => e.Properties["Type"] == "Website");
            DownloadBlock.LinkTo(ImageBlock, e => e.Properties["Type"] == "Thumbnail");

            MergeBlock.LinkTo(DiskWriteBlock);

            FinishBlock.LinkFrom(DiskWriteBlock, ImageBlock);
        }