DavidtheGoat123
DavidtheGoat123

Reputation: 23

How can I make a VBS Script that doesn't wait for a process to finish?

I'm new to VBS and I want to know how to make a part of the script work without waiting for the other part...I want to prank my friends with this simple script, but the message won't show up, while the first part works...is it possible?

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")

Any ideas? D:

Upvotes: 2

Views: 1573

Answers (2)

omegastripes
omegastripes

Reputation: 12612

Split your code up into separate procedures, that will be launched asynchronously, add some utility procedures, and launch them. Take a look at the below example:

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

This approach is very straightforward and allows to launch procedures in separate processes only. There is no possibility to control the processes flow of each other (eg terminate), nor to pass data from one to another. If you need such functionality you have to arrange some multiprocess environment (like in this asynchronous ping example).

Upvotes: 2

some1
some1

Reputation: 867

Your message is not shown because your code block from 1st line (do) until the 3rd last line (loop) is an endless loop, you will need to first decide when the message box should be displayed, should it be like below instead? Also, which step that you don't want it to wait for? For that step, you can follow what @dbmitch suggested above.

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

Upvotes: 0

Related Questions