Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
WebClient.DownloadFileAsync Cancel not working
Message
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 8.0
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Miscellaneous
Thread ID:
01300799
Message ID:
01301091
Views:
13
This message has been marked as the solution to the initial question of the thread.
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
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform