'
' Example script for 2BrightSparks SyncBackPro V8 that waits for a program to
' finish before running the profile. If its a simulated run, or a restore,
' then it can be run regardless.
'
' SBLang=Basic
'
' http://www.2BrightSparks.com/
'

' The name of the process you want to have finished running
const ProcessName = "Notepad.exe"

' The number of seconds to wait before re-checking if it is still running
const RetrySecs = 5

'-----------------------------------------------------------------------------
'-DO NOT MODIFY ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING !-
'-----------------------------------------------------------------------------

'
' This is a runtime script 
'
Function Description(ByRef ScriptType)
  Description = "Wait for " & ProcessName & " to finish before starting profile"
  ScriptType = SCRIPTTYPE_RUN
End Function

'
' Remind user to edit this script before use
'
Function Install(Interactive)
  If Interactive Then
    MsgBox("Please edit the script file " & SBSystem.ScriptFilename & " before you use it.")
  End If
End Function

'
'
Function RunDisabledCheck(NoLog)
  ' Running simulated or as a restore?
  If SBRunning.Restore or SBRunning.Simulated then
    RunDisabledCheck = ""
    NoLog = FALSE
    Exit Function
  End If

  SBSystem.UpdateFileStatus("Waiting for " & ProcessName & "...")

  ' Create object to tell us what processes are running
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set objShell = CreateObject("WScript.Shell")

  Do
    ' Is the process running? Could be more than one process.
    Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = '" & ProcessName & "'")
    RunProfile = (colProcessList.Count < 1)

    If RunProfile Then
      ' The program is not running
      RunDisabledCheck = ""
      NoLog = FALSE
      Exit Do
    Else
      ' The program is (still) running
      RunDisabledCheck = "The program is still running"
      NoLog = TRUE
    End If

    ' Have we been told to abort?
    if SBRunning.Abort then
      ' Let SyncBack see that the user aborted
      RunDisabledCheck = ""
      NoLog = FALSE
      Exit Do
    End If

    ' Wait RetrySecs seconds and try again
    if SBRunning.Sleep(RetrySecs) then
      ' User has aborted profile
      RunDisabledCheck = ""
      NoLog = FALSE
      Exit Do
    end if
  Loop While 1=1

  Set objShell = nothing
End Function
