如何创建一个不等待进程完成的VBS脚本?

时间:2016-08-07 18:44:42

标签: vbscript

我是VBS的新手,我想知道如何在不等待其他部分的情况下让脚本的一部分工作......我想用这个简单的脚本恶作剧我的朋友,但是消息不会显示虽然第一部分有效但是可能吗?

do
  Dim ts
  Dim strDriveLetter
  Dim intDriveLetter
  Dim fs 'As Scripting.FileSystemObject
  Const CDROM = 4
  On Error Resume Next
  Set fs = CreateObject("Scripting.FileSystemObject")
  strDriveLetter = ""
  For intDriveLetter = Asc("A") To Asc("Z")
    Err.Clear
    If fs.GetDrive(Chr(intDriveLetter)).DriveType = CDROM Then
      If Err.Number = 0 Then
        strDriveLetter = Chr(intDriveLetter)
        Exit For
      End If
    End If
  Next
  Set oWMP = CreateObject("WMPlayer.OCX.7" )
  Set colCDROMs = oWMP.cdromCollection
  For d = 0 to colCDROMs.Count - 1
    colCDROMs.Item(d).Eject
  Next 'null

  For d = 0 to colCDROMs.Count - 1
    colCDROMs.Item(d).Eject
  Next 'null

  set owmp = nothing
  set colCDROMs = nothing
loop

x=msgbox("Message here" ,48, "Blah")

有什么想法吗? d:

2 个答案:

答案 0 :(得分:1)

将代码拆分为单独的过程,这些过程将异步启动,添加一些实用程序并启动它们。看看下面的例子:

CheckTask ' always at the begining of the script

' this code is executed once in initial script

LaunchTask "EjectCdRoms" ' friendly keep CD-ROMs opened
LaunchTask "Msg" ' friendly show the message

' procedures to be launched asynchronously

Sub EjectCdRoms()
    Dim oWMP
    Dim cCDROMs
    Dim i
    On Error Resume Next
    Set oWMP = CreateObject("WMPlayer.OCX.7")
    Do
        Set cCDROMs = oWMP.cdromCollection
        For i = 0 To cCDROMs.Count - 1
            cCDROMs.Item(d).Eject
        Next
        WScript.Sleep 500
    Loop
End Sub

Sub Msg()
    Dim x
    x = MsgBox("Message here", 48, "Blah")
End Sub

' utility procedures, do not modify

Sub CheckTask()
    If WScript.Arguments.Named.Exists("task") Then
        Execute WScript.Arguments.Named.Item("task")
        WScript.Quit
    End If
End Sub

Sub LaunchTask(sTaskName)
    CreateObject("WScript.Shell").Exec """" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task:" & sTaskName & """"
End Sub

这种方法非常简单,只允许在单独的进程中启动过程。不可能控制彼此的处理流程(例如终止),也不可能将数据从一个传递到另一个。如果您需要这样的功能,则必须安排一些多进程环境(如此asynchronous ping example)。

答案 1 :(得分:-1)

您的消息未显示,因为您的代码阻止从第1行(执行)到第3行最后一行(循环)是无限循环,您需要先确定消息框何时应该是显示,是否应该像下面那样?此外,您不希望它等待哪一步?对于该步骤,您可以按照上面提到的@dbmitch建议。

do
  .
  .
  .
  x=msgbox("Message here" ,48, "Blah")
loop