0
Public Sub downloadFile(ByVal url As String)
        LogFile.displayMessage("add url=" & url)
        downloadURLs.Enqueue(url)

        If t Is Nothing Then
            LogFile.displayMessage("create new thread")
            t = New Thread(AddressOf ThreadedDownloadFile)
        End If

        If Not t.IsAlive Then
            LogFile.displayMessage("start thread")
            t.Start()
        End If

    End Sub


 Private Sub download()
        LogFile.displayMessage("thread running count=" & downloadURLs.Count)
        If downloadURLs.Count > 0 Then
            Dim client = New WebClient()
            AddHandler client.DownloadFileCompleted, AddressOf downloadFileCompleted
            AddHandler client.DownloadProgressChanged, AddressOf downloadProgressChanged

            Dim url = downloadURLs.Dequeue()
            LogFile.displayMessage("downloading url=" & url)
            url = WebRequest.Create(url).GetResponse.ResponseUri.AbsoluteUri
            Dim fileName = url.Substring(url.LastIndexOf("/") + 1)

            progress = 0
            LogFile.displayMessage("downloading NOW NOW NOW url=" & url)
            client.DownloadFile(New Uri(url), localAddress & fileName)
        End If
        LogFile.displayMessage("thread end")
    End Sub
    Private Sub downloadFileCompleted() 'ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
        LogFile.displayMessage("download complete ") ' & e.Result)

        LogFile.displayMessage("set next download going")
        download()
    End Sub

但在第二轮它会获取 url,然后在下载文件处停止。它也这样做downloadfileasync

我看到很多网页都在讨论这个问题,但没有一个能解决它

有谁知道如何解决这个问题?

额外细节

我确实开始了一个新线程,但无论我如何做,webclient.downloadfile 或 downloadfileasync 等它们第一次看起来都很好,但是在第二次调用时,它们似乎确实会在文件出现时下载,但是永远不会进行进度调用,并且永远不会返回完成。

我试过处理,然后什么都没有,然后重新声明它,甚至在处理后强制垃圾收集,什么都没有。

但它仍然不想工作。

我正在探索自己通过流进行传输

4

2 回答 2

1

我建议该线程仍然处于活动状态,因为您不检查该情况。

尝试如下更改代码以测试我的理论:

If Not t.IsAlive Then
    LogFile.displayMessage("start thread")
    t.Start()
else
    LogFile.DisplayMessage("Thread was still active")
End If

我本来以为您每次都想开始一个新线程?

于 2012-08-08T13:03:33.517 回答
0

问题与下载无关,是这段代码:-

Dim wrequest = WebRequest.Create(url) Dim wresponce = wrequest.GetResponse()

    'save new url to be returned
    return wresponce.ResponseUri.AbsoluteUri

我现在在使用后调用 close 和 null 实例。

'making sure to close off unneeded resources after use wresponce.Close() wresponce = Nothing wrequest = Nothing

于 2012-08-14T11:36:59.347 回答