[英]How do I wait for script to finish?
我有三個腳本,由下面的函數調用。 當第一個腳本仍在運行時,會調用后續命令,這會阻止我的第一個命令完成。
public static void InvokePowerShellCommand(this string command)
{
var powerShell = PowerShell.Create();
powerShell.AddScript(command).Invoke();
}
我想確保腳本在繼續下一個之前完成,我怎樣才能做到這一點?
向調用添加一個事件處理程序,一個非常簡單的例子是
private static PSInvocationState state;
public static void InvokePowerShellCommand(this string command)
{
state = PSInvocationState.NotStarted;
PowerShell ps = PowerShell.Create();
ps.InvocationStateChanged += new EventHandler<PSInvocationStateChangedEventArgs>(InvocationStateChanged);
ps.AddScript(command);
ps.Invoke();
while (state == PSInvocationState.Running)
System.Threading.Thread.Sleep(100);
}
static void InvocationStateChanged(object sender, PSInvocationStateChangedEventArgs e)
{
state = e.InvocationStateInfo.State;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.