Example #1
0
        internal ConsoleStream(ProcessStartInfo startInfo, IStreamMapper <T> mapper, CancellationToken token)
        {
            _token  = token;
            _mapper = mapper;

            _process = new Process
            {
                StartInfo           = startInfo,
                EnableRaisingEvents = true
            };

            if (CancellationToken.None != token)
            {
                token.Register(CancelProcess);
            }

            _process.OutputDataReceived += (sender, args) =>
            {
                var val = _mapper.OnData(args.Data, false);
                if (null != val)
                {
                    _values.Add(val);
                }
                Error = _mapper.Error;
            };

            _process.ErrorDataReceived += (sender, args) =>
            {
                var val = _mapper.OnData(args.Data, true);
                if (null != val)
                {
                    _values.Add(val);
                }
                Error = _mapper.Error;
            };

            _process.Exited += (sender, args) =>
            {
                Error      = _mapper.Error;
                IsSuccess  = _process.ExitCode == 0;
                IsFinished = true;
                _values.Add(_mapper.OnProcessEnd(_process.ExitCode));
            };

            if (!_process.Start())
            {
                throw new FluentDockerException($"Could not start process {startInfo.FileName}");
            }

            _process.BeginOutputReadLine();
            _process.BeginErrorReadLine();
        }