开发者

powershell throws PipelineStoppedException for an async call

Below code throws PipelineStoppedException when it tries to end invoke. Could anybody see anything wrong? Thanks.

        using (PowerShell powershell = PowerShell.Create())
        {                
                powershell.AddScript(script);
                powershell.Runspace = CreateRunspace();
                lock (powershell.Runspace)
                {
                    powershell.BeginInvoke(
                                input,
                                setting,
                                delegate(IAsyncResult result)
                                {                                       
                                  powershell.EndInvoke(result); // throw pipeline stopped exception.                                         
  开发者_JS百科                              },
                               null);
                }
        }


BeginInvoke returns immediately - by design - so the using clause closes, disposing the powershell instance before EndInvoke gets called. Use regular synchronous Invoke. You are mixing up sync and async patterns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜