Vb.net 如何在调用另一个函数之前等待命令行活动完成?

Vb.net 如何在调用另一个函数之前等待命令行活动完成?,vb.net,powershell,batch-file,command-line,vbscript,Vb.net,Powershell,Batch File,Command Line,Vbscript,这是我的密码。我们为msbuild进程使用一组命令。根据环境的不同,命令和其他活动略有不同。所以,我想出了如下的办法。但是,有些命令需要一段时间(大约1米)才能完成。我的剧本等不及了。而且,MoveStagedFolders()函数不会启动。我单独执行了这个函数,它工作正常。所以我的问题是: 为什么未调用MoveStagedFolders() 如何等待命令行活动完成 Option Explicit Dim wshShell, environment, strBuildDirect

这是我的密码。我们为msbuild进程使用一组命令。根据环境的不同,命令和其他活动略有不同。所以,我想出了如下的办法。但是,有些命令需要一段时间(大约1米)才能完成。我的剧本等不及了。而且,MoveStagedFolders()函数不会启动。我单独执行了这个函数,它工作正常。所以我的问题是:

  • 为什么未调用MoveStagedFolders()
  • 如何等待命令行活动完成

        Option Explicit
        Dim wshShell, environment, strBuildDirectory, strWebsiteDirectory
        On Error Resume Next
        strBuildDirectory = "someFoldersPath\*"
        strWebsiteDirectory = "\\DestinationPath\"
        environment = InputBox("Please Enter the Environment.")
    
        if  Trim(environment) <> "" then
            Set wshShell = wscript.CreateObject("wscript.Shell")
            wshShell.Run "C:\Windows\system32\cmd.exe" 
            WScript.Sleep 500   
            wshShell.sendkeys "cd c:\Program Files +9x86+0\Microsoft Visual Studio 13.0\Common7\Tools"
            wshShell.sendkeys "{ENTER}"
            wshShell.sendkeys "vsvars32.bat"
            wshShell.sendkeys "{ENTER}"
            wshShell.sendkeys "cd c:\working\develop"
            wshShell.sendkeys "{ENTER}"
            wshShell.sendkeys "msbuild/t:clean"
            wshShell.sendkeys "{ENTER}"
            wshShell.sendkeys "msbuild/p:configuration=" & environment
            wshShell.sendkeys "{ENTER}" 
            'I need to wait about a minute here.
            if UCase(environment) = "QA" then
                'Restart IIS
                wshShell.sendkeys "iisreset /restart localhost"
                wshShell.sendkeys "{ENTER}"
    
            elseif environment = "123" then
                'I need to move some folders to shared folder
                Call MoveStagedFolders()
                'Wait until the moving is done
                if MoveStagedFolders = true then
                    'MsgBox "Restarting IIS"
                    wshShell.sendkeys "iisreset /restart devIp"
                    wshShell.sendkeys "{ENTER}"
                end if
            end if
        else
            MsgBox "No environment was entered. Build may not succeed."
        end if
    
        Function MoveStagedFolders()
            With CreateObject("Scripting.FileSystemObject")
                'Need to overwrite folders
                .CopyFolder strBuildDirectory, strWebsiteDirectory, true
            End With
            MoveStagedFolders = true
        End Function
    
    选项显式
    Dim wshShell、环境、strBuildDirectory、strWebsiteDirectory
    出错时继续下一步
    strBuildDirectory=“someFoldersPath\*”
    strWebsiteDirectory=“\\DestinationPath\”
    环境=输入框(“请输入环境”)
    如果修剪(环境)”,则
    设置wshShell=wscript.CreateObject(“wscript.Shell”)
    运行“C:\Windows\system32\cmd.exe”
    WScript.Sleep 500
    wshShell.sendkeys“cd c:\Program Files+9x86+0\Microsoft Visual Studio 13.0\Common7\Tools”
    wshShell.sendkeys“{ENTER}”
    wshShell.sendkeys“vsvars32.bat”
    wshShell.sendkeys“{ENTER}”
    wshShell.sendkeys“cd c:\working\developer”
    wshShell.sendkeys“{ENTER}”
    wshShell.sendkeys“msbuild/t:clean”
    wshShell.sendkeys“{ENTER}”
    wshShell.sendkeys“msbuild/p:configuration=”环境(&E)
    wshShell.sendkeys“{ENTER}”
    “我需要在这里等一分钟左右。
    如果UCase(环境)=“QA”,则
    '重新启动IIS
    wshShell.sendkeys“iisreset/restart localhost”
    wshShell.sendkeys“{ENTER}”
    elseif environment=“123”然后
    '我需要将一些文件夹移动到共享文件夹
    调用MoveStagedFolders()
    “等待移动完成
    如果MoveStagedFolders=true,则
    “MsgBox”正在重新启动IIS
    wshShell.sendkeys“iisreset/restart devIp”
    wshShell.sendkeys“{ENTER}”
    如果结束
    如果结束
    其他的
    MsgBox“未输入任何环境。生成可能无法成功。”
    如果结束
    函数MoveStagedFolders()
    使用CreateObject(“Scripting.FileSystemObject”)
    '需要覆盖文件夹吗
    .CopyFolder strBuildDirectory,strWebsiteDirectory,true
    以
    MoveStagedFolders=true
    端函数
    

  • 首先,不要为此使用
    SendKeys
    。将所有命令放在批处理文件中并调用该文件

    其次,您可以使用
    START
    命令使批处理文件等待命令完成,如下所示:

    START /WAIT MyProgram.exe
    

    我不认为
    SendKeys
    是你最好的选择。能否将所有命令放入
    BAT
    文件中,并从VBScript调用该批处理文件?如果使用
    Run
    函数调用它,并将
    True
    作为第三个参数传递,则VBScript将等待批处理文件完成后再继续。我知道这不是最佳选项。我当然要试一试。谢谢你的快速回答