.NETで非同期ファイルダウンロード

Private WithEvents _WebClient As New System.Net.WebClient

...

'--------------------------------------------------------------------------------
'「ダウンロード開始」ボタンクリック
'--------------------------------------------------------------------------------
Private Sub btnDownload_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles btnDownload.Click

    ''入力項目チェック
    If (txtDownloadUrl.Text.Length = 0) Then
        MessageBox.Show("ダウンロードURLを入力してください。", "エラー", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtDownloadUrl.Focus()
        Exit Sub
    End If
    If (txtSaveFilePath.Text.Length = 0) Then
        MessageBox.Show("保存先を入力してください。", "エラー", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtSaveFilePath.Focus()
        Exit Sub
    End If
    ''ファイル有無チェック
    If (My.Computer.FileSystem.FileExists(txtSaveFilePath.Text) = True) Then
        If MessageBox.Show("ファイルが既に存在します。上書きしますか?", "確認", _
                           MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = _
                           Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If
    End If
    ''ダウンロード開始
    Dim uni As New Uri(txtDownloadUrl.Text)
    Try
        _WebClient.DownloadFileAsync(uni, txtSaveFilePath.Text)
        txtDownloadUrl.ReadOnly = True
        txtSaveFilePath.ReadOnly = True
        btnDownload.Enabled = False
        btnCancel.Enabled = True
    Catch ex As Exception
        MessageBox.Show(ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

End Sub

...

'--------------------------------------------------------------------------------
'ダウンロード進捗状況変更イベント
'--------------------------------------------------------------------------------
Private Sub _WebClient_DownloadProgressChanged(ByVal sender As Object, _
                            ByVal e As System.Net.DownloadProgressChangedEventArgs) _
                            Handles _WebClient.DownloadProgressChanged
    pgbarDownload.Value = e.ProgressPercentage
    lblGetBytes.Text = e.BytesReceived.ToString & " / " & e.TotalBytesToReceive.ToString
End Sub

...

'--------------------------------------------------------------------------------
'ダウンロード完了イベント
'--------------------------------------------------------------------------------
Private Sub _WebClient_DownloadFileCompleted(ByVal sender As Object, _
                            ByVal e As System.ComponentModel.AsyncCompletedEventArgs) _
                            Handles _WebClient.DownloadFileCompleted
    If (e.Cancelled = True) Then
        lblGetBytes.Text = "ダウンロードは中止されました"
    ElseIf (e.Error Is Nothing) Then
        lblGetBytes.Text = "ダウンロードが完了しました"
    Else
        lblGetBytes.Text = e.Error.Message
    End If
    pgbarDownload.Value = 0
    txtDownloadUrl.ReadOnly = False
    txtSaveFilePath.ReadOnly = False
    btnDownload.Enabled = True
    btnCancel.Enabled = False
End Sub


ネタ元