为什么webclient不能在第二次调用时下载VB.NET
问题描述:
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不能在第二次调用时下载VB.NET
我看到很多网页讨论这个,但没有一样解决它
没有人知道这可怎么固定?
额外的细节
我做启动一个新的线程,但无论他们似乎都工作带来极大的第一次,但随后在第二个电话我该怎么办呢,webclient.downloadfile或downloadfileasync等 ,他们看起来似乎是在文件出现时下载的,但进度调用永远不会完成,并且永远不会完成。
我试过配置,然后什么都没有,然后重新声明它,甚至强制垃圾收集处理后,什么也没有。
但它仍然不想工作。
我正在探索在做转移自己,通过物流
答
的问题是无关的下载,这是该代码: -
Dim wrequest = WebRequest.Create(url) Dim wresponce = wrequest.GetResponse()
'save new url to be returned
return wresponce.ResponseUri.AbsoluteUri
我现在请密切与空的情况下,后有使用。
'making sure to close off unneeded resources after use wresponce.Close() wresponce = Nothing wrequest = Nothing
答
我建议螺纹仍处于活动状态,你不检查这一条件。
试着改变你的代码如下所示,以测试我的理论:
If Not t.IsAlive Then
LogFile.displayMessage("start thread")
t.Start()
else
LogFile.DisplayMessage("Thread was still active")
End If
我还以为你会希望每次反正启动一个新线程?
遗憾的布局,我不能让代码标签的工作,如果有些可以解释他们是如何工作我会改正。 – f1wade 2012-08-08 12:06:02