Example #1
0
        /// <summary>
        ///     Runs the specified process in a new thread and allows it to generate data by writing it to a stream, while
        ///     returning a stream that can be used to consume the data by reading from it.</summary>
        /// <param name="writer">
        ///     An action that generates data and writes it to a stream.</param>
        public static Stream CreateFromWriter(Action <Stream> writer)
        {
            // Everything the writer writes will be stored in here until the reader consumes it
            var info = new costreamInfo();

            // Start the writing action in a new thread
            var thread = new Thread(() =>
            {
                try
                {
                    writer(new writingCostream {
                        Info = info
                    });
                }
                catch (Exception e)
                {
                    info.Exception = e;
                }
                info.Ended = true;
                Monitor.PulseAll(info);
            });

            thread.Start();
            return(new readingCostream {
                Info = info
            });
        }
        /// <summary>
        ///     Runs the specified process in a new thread and allows it to generate data by writing it to a stream, while
        ///     returning a stream that can be used to consume the data by reading from it.</summary>
        /// <param name="writer">
        ///     An action that generates data and writes it to a stream.</param>
        public static Stream CreateFromWriter(Action<Stream> writer)
        {
            // Everything the writer writes will be stored in here until the reader consumes it
            var info = new costreamInfo();

            // Start the writing action in a new thread
            var thread = new Thread(() =>
            {
                try
                {
                    writer(new writingCostream { Info = info });
                }
                catch (Exception e)
                {
                    info.Exception = e;
                }
                info.Ended = true;
                Monitor.PulseAll(info);
            });
            thread.Start();
            return new readingCostream { Info = info };
        }