【问题标题】:why does webclient not download on second call VB.NET为什么 webclient 在第二次调用 VB.NET 时不下载
【发布时间】:2012-08-08 12:05:13
【问题描述】:
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 等。 第一次它们似乎都工作得很好,但在第二次调用时,它们似乎在文件出现时下载,但从未进行进度调用,也永远不会返回完成。

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

但它仍然不想工作。

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

【问题讨论】:

  • 对不起,我无法让代码标签工作,如果有人能解释它们是如何工作的,我会修复。

标签: .net vb.net multithreading webclient


【解决方案1】:

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

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

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

我还以为你每次都想开始一个新线程?

【讨论】:

    【解决方案2】:

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

    Dim wrequest = WebRequest.Create(url) 暗淡 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 2020-08-19
      • 1970-01-01
      • 2013-04-08
      • 2020-02-07
      相关资源
      最近更新 更多