开发者

powershell throws PipelineStoppedException for an async call

开发者 https://www.devze.com 2023-02-01 04:30 出处:网络
Below code throws PipelineStoppedException when it tries to end invoke. Could anybody see anything wrong? Thanks.

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

精彩评论

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