user1785594
user1785594

Reputation:

Abort DownloadFileAsync if the download is not progressed for 30 seconds

I'm downloading many files with webclient.downloadfileasync. I have set the maximumconnection to 10, so only 10 files can be downloaded at the same time. Sometimes files are missing from the otherside. When that happens, DownloadFileAsync just waits for the missing files to be downloaded(I don't know how long). I want to set a time limit so if the file downloading is not progressed for longer than 30 seconds, it should be canceled so it can move on to downloading the other files.

What should I do?

     Dim wc As WebClient = New WebClient

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

I have no idea after this. I think I should set up a 30sec timer somehow for each downloadfileasync and wc.abort() if it DLprogresschanged is false for 30 sec but I have no idea about how to do that. Please help me.

Upvotes: 0

Views: 323

Answers (1)

David Sdot
David Sdot

Reputation: 2333

Answer from GlennG here: How to change the timeout on a .NET WebClient object

With this class you can set a timeout for the connection. You need to add a AsyncCompletedEventHandler for the DownloadFileCompleted event and work with the result in it.

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

Upvotes: 0

Related Questions