【问题标题】:DownloadFileCompleted event not being called未调用 DownloadFileCompleted 事件
【发布时间】:2018-03-03 17:46:50
【问题描述】:

我正在尝试从服务器下载文件,但未调用“DownloadFileCompleted”事件。 有人知道为什么吗?

来源:

Public Event DownloadFileCompleted As AsyncCompletedEventHandler

Public Sub DownloadCompleted(sender As Object, e As AsyncCompletedEventArgs)
    MsgBox("Downloaded")
End Sub

Private Function DLOAD()
    Try
        Dim WebClientT As New System.Net.WebClient()
        AddHandler WebClientT.DownloadFileCompleted, AddressOf DownloadFileCompletedD
        ByteArray = WebClientT.DownloadData("https://ip/file.filetype")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Function

【问题讨论】:

    标签: vb.net network-programming webclient


    【解决方案1】:

    仅当您使用“DownloadFileAsync”时才会调用该事件,因为您正在同步下载线程将等待下载所有字节而不调用该事件。那么你不需要这个事件。完成下载将始终位于 DownloadData 下方。

    如果你调用这个方法使用

    AddHandler WebClientT.DownloadDataCompleted, AddressOf DownloadCompleted
    WebClientT.DownloadDataAsync(uri) 
    

    更多信息:

    https://msdn.microsoft.com/pl-pl/library/system.net.webclient.downloadfilecompleted(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

    【讨论】:

    • 我如何定义一个 Uri
    • 如果我这样做了,我会收到一条错误消息“表达式不产生值”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2014-03-06
    • 2015-11-14
    相关资源
    最近更新 更多