|
-
Feb 7th, 2011, 03:37 PM
#1
Thread Starter
Lively Member
[RESOLVED] Insert pop up error if connection drops or download fails
Hi,
I built a dwonloader program which download a file from the web.
I would like to insert an error message that pop up whenever the program fail to download the file and/or when the computer gets disconnected from the internet while the program is downloading the file.
Could some one please help me?
Thank you in advance,
Andrea
-
Feb 7th, 2011, 04:22 PM
#2
Re: Insert pop up error if connection drops or download fails
How exactly are you downloading the file? For synchronous methods you would wrap your code in a Try...Catch block and then display a MessageBox in the Catch part.
-
Feb 7th, 2011, 04:23 PM
#3
Re: Insert pop up error if connection drops or download fails
For connection network connectivity, you can handle the application event NetworkAvailabilityChanged. As for download failure, it's hard to suggest anything without knowing what method you use to download.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Feb 7th, 2011, 04:47 PM
#4
Thread Starter
Lively Member
Re: Insert pop up error if connection drops or download fails
Thanks for the answer,
I am using DownloadFileAsync to download the file.
Ihad already tryed that method with no luck:
Code:
Try
Download.DownloadFileAsync(New Uri("URL" & ComboBox1.Text & ".rar"), Textbox1.Text & "\" & ComboBox1.Text & ".rar")
Catch err As Exception
MsgBox("error" & err.Message)
End Try
What am I missing?
-
Feb 7th, 2011, 05:16 PM
#5
Re: Insert pop up error if connection drops or download fails
What you're missing is that DownloadFileAsync returns immediately while the file continues to get downloaded in the background. It will only throw an exception if the remote or local path is malformed or invalid. It doesn't actually check that the file can be downloaded.
You are presumably already handling the DownloadFileCompleted event. That event is raised whether the download was successfull or not. The e.Error property will contain the exception if one was thrown. If that's Nothing, the download was successful and you can access the local file.
-
Feb 7th, 2011, 05:57 PM
#6
Thread Starter
Lively Member
Re: Insert pop up error if connection drops or download fails
jmcilhinney:
are you saying saying I should add the "try" "catch" in the downloadfilecomplete event?
Right now there I only have this:
Code:
Private Sub Download_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles Download.DownloadFileCompleted
If ProgressBar1.Value = 100 Then
MsgBox("Download Completed!", vbInformation, "Info")
Label1.Text = "0%"
Timer1.Stop()
End If
End Sub
How would I implement it? I am confuse.
Stanav:
how would i handle the application event NetworkAvailabilityChanged? I cannot find it anywhere
-
Feb 7th, 2011, 09:09 PM
#7
Thread Starter
Lively Member
Re: Insert pop up error if connection drops or download fails
I am getting crazy about it.
I tried this:
Code:
Private Sub Download_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles Download.NetworkAvailabilityChanged
MsgBox("computer is no longer connected")
End Sub
But there is an error:
event 'NetwrokAvailabilityChanged' cannot be found.
I really have no clue
Last edited by Netmaster; Feb 7th, 2011 at 09:13 PM.
-
Feb 7th, 2011, 09:38 PM
#8
Thread Starter
Lively Member
Re: Insert pop up error if connection drops or download fails
I think I almsot did it:
Iadded this into my Form1_load:
Code:
AddHandler My.Computer.Network.NetworkAvailabilityChanged, AddressOf MyApplication_NetworkAvailabilityChanged
And then this:
Code:
Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs)
If Not My.Computer.Network.IsAvailable Then
MsgBox("Impossible to download! Your computer is not connected to the internet", vbCritical, "Attention!")
End If
End Sub
If the pc gets disconnected the pop up error comes up! The thing is that I just want the error to come up if the user is downloading a file and the the pc diconnects from the internet! But this error also come up if the pc get disconnected and the program is open and the user is not downloading anything.
I tryed to add this:
Code:
AddHandler My.Computer.Network.NetworkAvailabilityChanged, AddressOf MyApplication_NetworkAvailabilityChanged
in the
Code:
Private Sub Download_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles Download.DownloadProgressChanged
but the program freezes as soon as the download starts!
Do you know how can work this out?
Thanks,
Andrea
-
Feb 7th, 2011, 11:02 PM
#9
Re: Insert pop up error if connection drops or download fails
I wasn't suggesting that you add a Try...Catch block to your DownloadFileCompleted event handler. The exception handler is built into WebClient class. The WebClient tries to download the file and, if an exception is thrown, it catches it. Once the download is complete, either successfully or unsuccessfully, the DownloadFileCompleted event is raised. In the event handler, you check whether an exception occurred or not and you proceed accordingly, e.g.
vb.net Code:
Imports System.Net Imports System.ComponentModel Public Class Form1 Private WithEvents downloader As New WebClient Private Sub downloadButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles downloadButton.Click If Not Me.downloader.IsBusy Then MessageBox.Show("A download is already in progress.", "Download In Progress", MessageBoxButtons.OK, MessageBoxIcon.Error) Else Try Me.downloadButton.Enabled = False Dim remoteAddress As String = Me.remoteAddressTextBox.Text Dim localAddress As String = Me.localAddressTextBox.Text Dim userToken As String() = New String() {remoteAddress, localAddress} Me.downloader.DownloadFileAsync(New Uri(remoteAddress), localAddress, userToken) Catch ex As Exception Me.downloadButton.Enabled = True MessageBox.Show(ex.Message, "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub Private Sub downloader_DownloadFileCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) Handles downloader.DownloadFileCompleted Me.downloadButton.Enabled = True If e.Error Is Nothing Then Dim addresses As String() = DirectCast(e.UserState, String()) MessageBox.Show(String.Format("The file '{0}' was successfully downloaded to '{1}'.", addresses(0), addresses(1)), "Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Error) Else MessageBox.Show(e.Error.Message, "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub End Class
With regards to the NetworkAvailabilityChanged event, there may not be any reason to handle it as the DownloadFileCompleted event will still provide exception details, so you may end up double-reporting to the user. If you do still want to handle it, you can check the IsBusy property of your WebClient to determine whether a file is being downloaded.
-
Feb 7th, 2011, 11:35 PM
#10
Thread Starter
Lively Member
Re: Insert pop up error if connection drops or download fails
Thanks for the great explanation!
I just figured out how to set everything using the NetworkAvailabilityChanged event. I was about to write to the thread.
But since you explained this method to me I am going to check it out and probably implement it in the my code.
Can I ask you one more question?
is it possible to Pasuse and Resuming the download with Async?
I cannot find anything on the internet concerning this.
Thank you,
Andrea
-
Feb 7th, 2011, 11:37 PM
#11
Thread Starter
Lively Member
Re: Insert pop up error if connection drops or download fails
Thanks for the great explanation!
I just figured out how to set everything using the NetworkAvailabilityChanged event. I was about to write to the thread.
But since you explained this method to me I am going to check it out and probably implement it in the my code.
Can I ask you one more question?
is it possible to Pasuse and Resuming the download with Async?
I cannot find anything on the internet concerning this.
Thank you,
Andrea
-
Feb 8th, 2011, 12:17 AM
#12
Re: Insert pop up error if connection drops or download fails
 Originally Posted by Netmaster
is it possible to Pasuse and Resuming the download with Async?
No. If it's possible to do that, I'm sure that you would have to use a WebRequest, which gives you the most fine-grained control over web-related operations. I'm really not sure of the protocol though, so I'm not sure how it would be implemented. Resuming is something that must be supported by the server, so you should first investigate the protocol for that. Once you know that, you can then look into how to implement that protocol using a WebRequest. If it's possible, you'd have to include in the request the byte at which to start reading the source.
-
Feb 8th, 2011, 05:54 AM
#13
Thread Starter
Lively Member
Re: Insert pop up error if connection drops or download fails
Ok then!
I thought it was not that hardto do. I have seen some vb samples (which i tested and work) doing pausing and resuming (not using Async though).
I thought it would have been something l like:
-----------------------------------------------
Check file-size (in bytes) on the pc
Check file-size (in bytes) on the server
Start resuming from last byte of file in pc
-----------------------------------------------
Well jmcilhinney, thanks a lot for your help!
Regards,
Andrea
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|