我正在同步执行powershell命令。绑定一个eventhandler,它在管道发出数据时成功运行。当某些条件满足时,我希望停止执行powershell命令,但是当我调用pipeLine.Stop()时,程序只是无限期挂起。我可以知道出了什么问题吗?
Command shellCommand = new Command("Get-MsolUser");
Pipeline pipeLine = powerShellRunspace.CreatePipeline();
pipeLine.Commands.Add(shellCommand);
pipeLine.Input.Close();
pipeLine.Output.DataReady += delegate(object sender,EventArgs args) {
try {
PipelineReader<PSObject> output = sender as PipelineReader<PSObject>;
if (output != null) {
bool isCompleted = false;
while (output.Count > 0 && !isCompleted) {
PSObject pso = (PSObject)output.Read();
isCompleted = addDataDelegate(pso);
}
if (isCompleted) {
pipeLine.Stop(); //HANGS HERE
log("JUST STOPPED PIPELINE..");
}
}
} catch(Exception ex) {
log(ex.ToString());
}
};
pipeLine.Invoke();
答案 0 :(得分:0)
在异步处理管道输出的委托内同步停止管道可能是个坏主意。
也许 Pipeline.StopAsync 方法可以在此代码中更好地工作。