3

由于 StandardOutput 不是反应式的,我需要一种方法来观察它。我知道 Process 类在编写输出时会公开一个用于接收通知的事件,因此我使用此扩展方法来获取标准输出的 IObservable

public static class ProcessExtensions
{
    public static IObservable<string> StandardOutputObservable(this Process process)
    {
        process.EnableRaisingEvents = true;
        process.StartInfo.RedirectStandardOutput = true;

        var received = Observable.FromEventPattern<DataReceivedEventHandler,DataReceivedEventArgs>(
            handler => handler.Invoke,
            h => process.OutputDataReceived += h,
            h => process.OutputDataReceived -= h)
            .TakeUntil(Observable.FromEventPattern(
                h => process.Exited += h,
                h => process.Exited -= h))
            .Select(e => e.EventArgs.Data);

        process.BeginOutputReadLine();

        return received;

        /* Or if cancellation is important to you...
        return Observable.Create<string>(observer =>
            {
                var cancel = Disposable.Create(process.CancelOutputRead);

                return new CompositeDisposable(
                    cancel, 
                    received.Subscribe(observer));
            });
         */
    }
}

如发现here。但是当我开始这个过程时

public sealed class ProgramHelper
{
    private readonly Process _program = new Process();
    public IObservable<string> ObservableOutput { get; private set; }

    public ProgramHelper(string programPath, string programArgs)
    {
        _program.StartInfo.FileName = programPath;
        _program.StartInfo.Arguments = programArgs;
    }

    public void StartProgram()
    {
        ConfigService.SaveConfig(
            new Config(
                new Uri(@"http://some.url.com")));

        _program.Start();

        ObservableOutput = _program.StandardOutputObservable();

    }
}

...

[TestFixture]
public class When_program_starts
{
    private ProgramHelper _program;

    [Test]
    public void It_should_not_complain()
    {
       //W
       Action act = () => _program.StartProgram();
       //T
       act.ShouldNotThrow<Exception>();
    }
}

我收到此错误:

“StandardOut 尚未重定向或进程尚未开始。”

感谢您的时间。

编辑:将 ProgramHelper 编辑为

    public ProgramHelper(string programPath, string programArgs)
    {
        _program.StartInfo.FileName = programPath;
        _program.StartInfo.Arguments = programArgs;
        _program.EnableRaisingEvents = true;
        _program.StartInfo.UseShellExecute = false;
        _program.StartInfo.RedirectStandardOutput = true;
    }

但现在它抛出“访问被拒绝异常”。

看来我无权以编程方式启动该过程;如果我从控制台启动 exe,它就可以正常工作。

4

1 回答 1

3

在进程启动后,您正在改变Process.StartInfo属性。

Process.StartInfo MSDN 文档

您可以更改 StartInfo 属性中指定的参数,直到您在进程上调用 Start 方法。启动进程后,更改 StartInfo 值不会影响或重新启动关联的进程。

于 2012-08-20T15:27:45.327 回答