2014-12-23 1 views
0

webclient.downloadfileasync를 사용하여 많은 파일을 다운로드합니다. 최대 연결 수를 10으로 설정 했으므로 동시에 10 개의 파일 만 다운로드 할 수 있습니다. 파일이 다른 쪽에서 누락되는 경우가 있습니다. 이런 일이 생길 때, DownloadFileAsync는 누락 된 파일이 다운로드 될 때까지 기다립니다 (얼마나 오래 있는지 모르겠습니다). 시간 제한을 설정하여 의 파일 다운로드가 30 초 이상 진행되지 않으면 취소해야 다른 파일을 다운로드 할 수 있습니다.다운로드를 중단하십시오 DownloadFileAsync 다운로드가 30 초 동안 진행되지 않는 경우

어떻게해야합니까?

 Dim wc As WebClient = New WebClient 

         wc.DownloadFileAsync(Ur1, localFL) 
AddHandler wc.DownloadProgressChanged, Sub(sender As Object, e As DownloadProgressChangedEventArgs) 

이후에는 알 수 없습니다. DLprogresschanged가 30 초 동안 거짓이라면 각 downloadfileasync와 wc.abort()에 대해 어떻게 든 30 초 타이머를 설정해야한다고 생각합니다.하지만 그 방법을 알지 못합니다. 도와주세요. 여기 GlennG에서

답변

1

답변 : 연결에 대한 시간 제한을 설정할 수 있습니다이 클래스와 How to change the timeout on a .NET WebClient object

. DownloadFileCompleted 이벤트에 AsyncCompletedEventHandler를 추가하고 그 결과로 작업해야합니다.

Public Class WebClient 
Inherits System.Net.WebClient 

Private _TimeoutMS As Integer = 10000 

Public Sub New() 
    MyBase.New() 
    'MyBase.Encoding = System.Text.Encoding.UTF8 
End Sub 
Public Sub New(ByVal TimeoutMS As Integer) 
    MyBase.New() 
    _TimeoutMS = TimeoutMS 
    'MyBase.Encoding = System.Text.Encoding.UTF8 
End Sub 
''' <summary> 
''' Set the web call timeout in Milliseconds 
''' </summary> 
''' <value></value> 
Public WriteOnly Property setTimeout() As Integer 
    Set(ByVal value As Integer) 
     _TimeoutMS = value 
    End Set 
End Property 

Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest 
    Dim w As System.Net.HttpWebRequest = CType(MyBase.GetWebRequest(address), HttpWebRequest) 
    If _TimeoutMS <> 0 Then 
     w.Timeout = _TimeoutMS 
    End If 
    Return w 
End Function 

End Class 
관련 문제