Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
WebClient.DownloadFileAsync Cancel not working
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 8.0
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Divers
Thread ID:
01300799
Message ID:
01301110
Vues:
13
Thanks Rick,

I thought it seemed too good to be true. I could not get my head around how this could work but I saw nothing in the docs that would suggest it wouldn't. We will study your whitepaper and examples carefully.

Glenn

>You have to be very careful with async code like this in ASP.NET. What you're doing is essentially this:
>
>Load a page
>Fire an async event
>Exit the page (page is done immediately)
>
>When the async call returns the page is likely gone and so your event goes nowhere (actually it will fail but because the thread and page is already done processing you don't see the error) or worse tries to access control and HttpContext intrinsic objects that are no longer available... Either way the code you have below will bomb and certainly not return the appropriate result because it will immeidately exit and not wait for the callback.
>
>There are official ways to run async tasks in ASP.NET however. Using the Async Page patter and Async Page Tasks allow you to run async operations that return to the current page in the context of the page.
>
>I have a set of slides and samples from a session I've been doing, regarding long running requests that might help.
>
>http://www.west-wind.com/WebLog/posts/186419.aspx
>
>+++ Rick ---
>
>
>>We have developed a WebForm to allow users to download updates from our website. So far the download process is going well. We do have a problem with WebClient.Cancel however. When issued we never receive an event at our handler (DownloadFileComplete) and to make matters worse the download continues until the file is completely processed.
>>
>>
>>  Public Sub GetFile(ByVal fName As String, ByVal ftpPath As String, ByVal locPath As String)
>>        Dim client As New WebClient
>>        AddHandler client.DownloadFileCompleted, AddressOf DownloadFileComplete
>>        '  Specify a progress notification handler.
>>        AddHandler client.DownloadProgressChanged, AddressOf DownloadProgress
>>        Try
>>            client.Credentials = New NetworkCredential("upgrades", "jamboree8035")
>>
>>            client.DownloadFileAsync(UriPath, locPath)
>>        Finally
>>            'Response.Redirect("default.aspx")
>>        End Try
>>    End Sub
>>
>>   Public Sub DownloadFileComplete(ByVal sender As Object, ByVal e As DownloadDataCompletedEventArgs)
>>        Response.Redirect("~/default.aspx")
>>    End Sub
>>
>>    Sub DownloadProgress(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
>>        Me.Label1.Text = e.ProgressPercentage.ToString
>>    End Sub
>>
>>    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
>>        Dim client As New WebClient
>>        client.CancelAsync()
>>        client.Dispose()
>>    End Sub
>>
>>
>>Any ideas?
>>
>>Thanks
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform