Results 1 to 13 of 13

Thread: [RESOLVED] Insert pop up error if connection drops or download fails

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    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?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Imports System.Net
    2. Imports System.ComponentModel
    3.  
    4. Public Class Form1
    5.  
    6.     Private WithEvents downloader As New WebClient
    7.  
    8.     Private Sub downloadButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles downloadButton.Click
    9.         If Not Me.downloader.IsBusy Then
    10.             MessageBox.Show("A download is already in progress.",
    11.                             "Download In Progress",
    12.                             MessageBoxButtons.OK,
    13.                             MessageBoxIcon.Error)
    14.         Else
    15.             Try
    16.                 Me.downloadButton.Enabled = False
    17.  
    18.                 Dim remoteAddress As String = Me.remoteAddressTextBox.Text
    19.                 Dim localAddress As String = Me.localAddressTextBox.Text
    20.                 Dim userToken As String() = New String() {remoteAddress, localAddress}
    21.  
    22.                 Me.downloader.DownloadFileAsync(New Uri(remoteAddress), localAddress, userToken)
    23.             Catch ex As Exception
    24.                 Me.downloadButton.Enabled = True
    25.  
    26.                 MessageBox.Show(ex.Message, "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
    27.             End Try
    28.         End If
    29.     End Sub
    30.  
    31.     Private Sub downloader_DownloadFileCompleted(ByVal sender As Object,
    32.                                                  ByVal e As AsyncCompletedEventArgs) Handles downloader.DownloadFileCompleted
    33.         Me.downloadButton.Enabled = True
    34.  
    35.         If e.Error Is Nothing Then
    36.             Dim addresses As String() = DirectCast(e.UserState, String())
    37.  
    38.             MessageBox.Show(String.Format("The file '{0}' was successfully downloaded to '{1}'.",
    39.                                           addresses(0),
    40.                                           addresses(1)),
    41.                             "Download Complete",
    42.                             MessageBoxButtons.OK,
    43.                             MessageBoxIcon.Error)
    44.         Else
    45.             MessageBox.Show(e.Error.Message, "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
    46.         End If
    47.     End Sub
    48.  
    49. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    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

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Insert pop up error if connection drops or download fails

    Quote Originally Posted by Netmaster View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    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
  •  



Click Here to Expand Forum to Full Width