VB中异步的问题,如果中止异步和异步出错时怎样处理。
Option Explicit
Event DownloadProgress(CurBytes As Long, MaxBytes As Long, SaveFile As String)
Event DownloadError(SaveFile As String)
Event DownloadComplete(MaxBytes As Long, SaveFile As String)
Public downStat As Boolean
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
On Error Resume Next
Dim f() As Byte, fn As Long
If AsyncProp.BytesMax <> 0 Then
fn = FreeFile
f = AsyncProp.Value
Open AsyncProp.PropertyName For Binary Access Write As #fn
Put #fn, , f
Close #fn
Else
RaiseEvent DownloadError(AsyncProp.PropertyName)
End If
RaiseEvent DownloadComplete(CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
downStat = False
End Sub
Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
On Error Resume Next
If AsyncProp.BytesMax <> 0 Then
RaiseEvent DownloadProgress(CLng(AsyncProp.BytesRead), CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
downStat = True: Timer1.Enabled = True
End If
End Sub
Private Sub UserControl_Resize()
SizeIt
End Sub
Public Sub BeginDownload(url As String, SaveFile As String)
On Error GoTo ErrorBeginDownload
downStat = True
UserControl.AsyncRead url, vbAsyncTypeByteArray, SaveFile, vbAsyncReadForceUpdate
Timer1.Enabled = True
Exit Sub
ErrorBeginDownload:
downStat = False
MsgBox Err & "开始下载数据失败!" _
& vbCrLf & vbCrLf & "错误:" & Err.Description, vbCritical, "错误"
End Sub
Public Sub SizeIt()
On Error GoTo ErrorSizeIt
With UserControl
.Width = ScaleX(32, vbPixels, vbTwips)
.Height = ScaleY(32, vbPixels, vbTwips)
End With
Exit Sub
ErrorSizeIt:
End Sub
这是一个下载的Activex OCX,不用任何控件就可从HTTP上下载文件,(一定要写在VB 的Activex OCX中!), 支持进度条。 但出错后就不能中止异步,或异步执行不知如何中止,望高手指点!!!!!!!!!!!!!!!!!