使用事件监视进度
使用多个接口可以实现事件处理程序来接收进度信息。 例如,可以将事件对象附加到数据编写器,以接收写入操作的状态。
事件处理程序在脚本中作为子例程实现。 以下示例演示如何定义子例程并使用 WScript.ConnectObject 方法将事件处理程序连接到对象。
' Create the MsftDiscFormat2Data object (see the IDiscFormat2Data interface).
Set dataWriter = CreateObject ("IMAPI2.MsftDiscFormat2Data")
' Attach event handler (see the DDiscFormat2DataEvents interface).
' The "dwBurnEvent_" parameter identifies the handler (see the
' dwBurnEvent_Update subroutine).
WScript.ConnectObject dataWriter, "dwBurnEvent_"
' Event handler for the MsftDiscFormat2Data.Write method.
' For details of the subroutine's parameters, see the
' DDiscFormat2DataEvents::Update method.
SUB dwBurnEvent_Update( byRef object, byRef progress )
' --- Other code occurs here.
END SUB
为事件处理程序名称指定的名称必须包含下划线后缀 (“_”) 。 若要形成子例程名称,请连接方法名称到事件处理程序名称。 例如,如果使用“dataWriterEvent_”作为事件处理程序名称,并且方法名称为“Update”,则子例程名称将dataWriterEvent_Update。
以下示例演示了将事件处理程序连接到对象的替代方法。
' Create object and connect the event handler in one step.
' Set dataWriter = _
' WScript.CreateObject("IMAPI2.MsftDiscFormat2Data","dwBurnEvent_")
' Event handler
SUB dwBurnEvent_Update( byRef object, byRef progress )
'--- Other code occurs here.
END SUB
如果系统包含要监视的第二个燃烧设备,则需要创建另一个 MsftDiscFormat2Data 对象和事件处理程序。
以下示例基于 “燃烧光盘图像 ”示例。 该示例将 ISO 映像写入空白光盘,并使用事件处理程序提供进度更新。
' This script burns data files to disc in a single session
' using files from a single directory tree.
' Copyright (C) Microsoft Corp. 2006
Option Explicit
' *** CD/DVD disc file system types
Const FsiFileSystemISO9660 = 1
Const FsiFileSystemJoliet = 2
Const FsiFileSystemUDF102 = 4
' *** IFormat2Data Write Action Enumerations
Const IMAPI_FORMAT2_DATA_WRITE_ACTION_VALIDATING_MEDIA = 0
Const IMAPI_FORMAT2_DATA_WRITE_ACTION_FORMATTING_MEDIA = 1
Const IMAPI_FORMAT2_DATA_WRITE_ACTION_INITIALIZING_HARDWARE = 2
Const IMAPI_FORMAT2_DATA_WRITE_ACTION_CALIBRATING_POWER = 3
Const IMAPI_FORMAT2_DATA_WRITE_ACTION_WRITING_DATA = 4
Const IMAPI_FORMAT2_DATA_WRITE_ACTION_FINALIZATION = 5
Const IMAPI_FORMAT2_DATA_WRITE_ACTION_COMPLETED = 6
const IMAPI_FORMAT2_DATA_WRITE_ACTION_VERIFYING = 7
WScript.Quit(Main)
Function Main
Dim Index ' Index to recording drive.
Dim Recorder ' Recorder object
Dim Path ' Directory of files to burn
Dim Stream ' Data stream for burning device
Index = 1 ' Second drive on the system
Path = "c:\test\imt\data\2tracks" ' Files to transfer to disc
' Create a DiscMaster2 object to connect to CD/DVD drives.
Dim g_DiscMaster
Set g_DiscMaster = WScript.CreateObject("IMAPI2.MsftDiscMaster2")
' Create a DiscRecorder object for the specified burning device.
Dim uniqueId
set recorder = WScript.CreateObject("IMAPI2.MsftDiscRecorder2")
uniqueId = g_DiscMaster.Item(index)
recorder.InitializeDiscRecorder( uniqueId )
' Create an image stream for a specified directory.
Dim FSI 'Disc file system
Dim Dir 'Root directory of the disc file system
Dim dataWriter
' Create a new file system image and retrieve root directory
Set FSI = CreateObject("IMAPI2FS.MsftFileSystemImage")
Set Dir = FSI.Root
' Define the new disc format and set the recorder
Set dataWriter = CreateObject ("IMAPI2.MsftDiscFormat2Data")
dataWriter.recorder = Recorder
dataWriter.ClientName = "IMAPIv2 TEST"
FSI.FreeMediaBlocks = dataWriter.FreeSectorsOnMedia
FSI.FileSystemsToCreate = FsiFileSystemISO9660
' Add the directory and its contents to the file system
Dir.AddTree Path, false
' Create an image from the file system
Dim result
Set result = FSI.CreateResultImage()
Stream = result.ImageStream
' Attach event handler to the data writing object.
WScript.ConnectObject dataWriter, "dwBurnEvent_"
' Specify the recorder and write the stream to disc.
WScript.Echo "Writing content to disc..."
dataWriter.write(Stream)
WScript.Echo "----- Finished writing content -----"
Main = 0
End Function
' Event handler - Progress updates when writing data
SUB dwBurnEvent_Update( byRef object, byRef progress )
DIM strTimeStatus
strTimeStatus = "Time: " & progress.ElapsedTime & _
" / " & progress.TotalTime
SELECT CASE progress.CurrentAction
CASE IMAPI_FORMAT2_DATA_WRITE_ACTION_VALIDATING_MEDIA
WScript.Echo "Validating media " & strTimeStatus
CASE IMAPI_FORMAT2_DATA_WRITE_ACTION_FORMATTING_MEDIA
WScript.Echo "Formatting media " & strTimeStatus
CASE IMAPI_FORMAT2_DATA_WRITE_ACTION_INITIALIZING_HARDWARE
WScript.Echo "Initializing Hardware " & strTimeStatus
CASE IMAPI_FORMAT2_DATA_WRITE_ACTION_CALIBRATING_POWER
WScript.Echo "Calibrating Power (OPC) " & strTimeStatus
CASE IMAPI_FORMAT2_DATA_WRITE_ACTION_WRITING_DATA
DIM totalSectors, writtenSectors, percentDone
totalSectors = progress.SectorCount
writtenSectors = progress.LastWrittenLba - progress.StartLba
percentDone = FormatPercent(writtenSectors/totalSectors)
WScript.Echo "Progress: " & percentDone & " " & strTimeStatus
CASE IMAPI_FORMAT2_DATA_WRITE_ACTION_FINALIZATION
WScript.Echo "Finishing the writing " & strTimeStatus
CASE IMAPI_FORMAT2_DATA_WRITE_ACTION_COMPLETED
WScript.Echo "Completed the burn."
CASE IMAPI_FORMAT2_DATA_WRITE_ACTION_VERIFYING
WScript.Echo "Verifying the data."
CASE ELSE
WScript.Echo "Unknown action: " & progress.CurrentAction
END SELECT
END SUB
相关主题