Results 1 to 26 of 26

Thread: [RESOLVED] Track progress of download, using My.Computer.Network.DownloadFile

  1. #1

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Hm... not sure if you can with that method.

    I know you can via a WebClient.

    Have you looked over the documentation?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Yeah I'm pretty sure you cant do it with that method, you need to 'manually' download the file (easiest way is probably with WebClient as weirddemon mentions) and basically just keep track of how many bytes you have downloaded so far and update the progress bar appropriately. I'm sure someone wrote a codebank article on it but I cant find it...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Anything in VB 2005 will work perfectly fine with VB 2010... but anyway I'll take a look and see if I can see what you are supposed to do with it
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Well I had a look - which part of it dont you get? There is a full working example included with it that shows you the FileDownloadComplete event in use:
    vb Code:
    1. 'FIRES WHEN DOWNLOAD IS COMPLETE
    2.     Private Sub _Downloader_FileDownloadComplete() Handles _Downloader.FileDownloadComplete
    3.         ProgBar.Value = ProgBar.Maximum
    4.         MessageBox.Show("File Download Complete")
    5.     End Sub

    You just add the WebFileDownloader.vb file to your project (Project -> Add Existing Item -> Browse to the file) and then you can use it in the exact same way as the example app does
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Quote Originally Posted by Emcrank View Post
    I did find this one in the codebank, BUT... it seems to be in like vb.net 2005 whereas im using 2010. I was looking threw it and it says RaiseEvent FileCompleted() and i couldn't find where this so called 'Event' was to check out how he found out when it was finished. Where can this 'Event' be found in the download he provides?
    this is the link: Here
    That's most likely a custom event he made. I'm sure there's a Class included in that project that you will need to reference in yours.

    Also, I think he's using a WebClient.

    *Edit: Chris was a second faster than me
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    In the only form that exists in the project you download from that thread that you posted a link to
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10

  11. #11

  12. #12
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Quote Originally Posted by Emcrank View Post
    I found wc.CancelAsync what does that do, would that cancel the download?
    I don't know. What happened when you tried it?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

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

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    With a WebClient:

    1. Call DownloadFileAsync to start the download
    2. Handle DownloadProgressChanged to track the progress
    3. Handle DownloadFileCompleted to know when the file has downloaded
    4. Call CancelAsync to cancel the download.

    Note that, because the download is asynchronous, the events will be raised on secondary threads. As such, you must use delegation to update the UI from those event handlers.
    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

  14. #14

  15. #15

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

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Are you aware that My.Computer.Network.DownloadFile can display a dialogue with a progress bar automatically? You can simply call that method and it will do all the work for you, displaying a modal dialogue with a progress bar and then returning when the download is complete. The only reason I can see not to do that is if you specifically want to include a ProgressBar in your own form, in which case I would think that an asynchronous download would be preferable.
    vb.net Code:
    1. Imports System.ComponentModel
    2. Imports System.Net
    3. Imports System.Threading
    4.  
    5. Public Class FileDownloader
    6.  
    7.     Private WithEvents downloader As New WebClient
    8.     Private synchroniser As SynchronizationContext
    9.  
    10.  
    11.     Public Property Source As String
    12.     Public Property Destination As String
    13.  
    14.  
    15.     Public Sub New()
    16.         'Events will be raised on the same thread that created the object.
    17.         Me.synchroniser = SynchronizationContext.Current
    18.     End Sub
    19.  
    20.     Public Sub New(ByVal source As String, ByVal destination As String)
    21.         Me.New()
    22.         Me.Source = source
    23.         Me.Destination = destination
    24.     End Sub
    25.  
    26.  
    27.     Public Function Download() As Boolean
    28.         Dim downloadCommenced As Boolean = False
    29.  
    30.         If Not Me.downloader.IsBusy Then
    31.             Me.downloader.DownloadFileAsync(New Uri(Me.Source), Me.Destination)
    32.             downloadCommenced = True
    33.         End If
    34.  
    35.         Return downloadCommenced
    36.     End Function
    37.  
    38.  
    39.     Public Event DownloadProgressChanged As EventHandler(Of DownloadProgressChangedEventArgs)
    40.     Public Event DownloadFileCompleted As EventHandler(Of AsyncCompletedEventArgs)
    41.  
    42.  
    43.     Private Sub downloader_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles downloader.DownloadProgressChanged
    44.         'Raise the DownloadProgressChanged event on the owning thread.
    45.         Me.synchroniser.Post(AddressOf OnDownloadProgressChanged, e)
    46.     End Sub
    47.  
    48.     Private Sub downloader_DownloadFileCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) Handles downloader.DownloadFileCompleted
    49.         'Raise the DownloadFileCompleted event on the owning thread.
    50.         Me.synchroniser.Post(AddressOf OnDownloadFileCompleted, e)
    51.     End Sub
    52.  
    53.  
    54.     Protected Overridable Sub OnDownloadProgressChanged(ByVal e As Object)
    55.         Debug.Assert(TypeOf e Is DownloadProgressChangedEventArgs)
    56.  
    57.         RaiseEvent DownloadProgressChanged(Me, DirectCast(e, DownloadProgressChangedEventArgs))
    58.     End Sub
    59.  
    60.     Protected Overridable Sub OnDownloadFileCompleted(ByVal e As Object)
    61.         Debug.Assert(TypeOf e Is AsyncCompletedEventArgs)
    62.  
    63.         RaiseEvent DownloadFileCompleted(Me, DirectCast(e, AsyncCompletedEventArgs))
    64.     End Sub
    65.  
    66. End Class
    Last edited by jmcilhinney; Jul 4th, 2010 at 09:12 PM. Reason: Changed conventional properties to auto-implemented properties.
    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

  17. #17

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

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Quote Originally Posted by Emcrank View Post
    How do i get it to display its own progressbar?
    It's a method. You call it, you pass it the appropriate arguments. If you don't know what arguments to pass then read the documentation. That's what it's for.
    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

  19. #19

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

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    This code:
    vb.net Code:
    1. Process.Start("Updater.exe", "http://junkfiles.bravehost.com/Location1/[Mozilla Firefox]Reconnecter.exe C:\Users\Aaron T\Documents My.Application.Info.AssemblyName")
    Is equivalent to typing this into a command prompt:
    Code:
    Updater.exe http://junkfiles.bravehost.com/Location1/[Mozilla Firefox]Reconnecter.exe C:\Users\Aaron T\Documents My.Application.Info.AssemblyName
    That's obviously wrong because, as you know, spaces are considered delimiters between arguments. If you wanted to run that at the command prompt you would have to quote the paths, i.e.
    Code:
    Updater.exe "http://junkfiles.bravehost.com/Location1/[Mozilla Firefox]Reconnecter.exe" "C:\Users\Aaron T\Documents" "My.Application.Info.AssemblyName"
    That's exactly what you need to do in your code too. To include a double quote in a literal string you must escape it with another double quote:
    vb.net Code:
    1. Process.Start("Updater.exe", """http://junkfiles.bravehost.com/Location1/[Mozilla Firefox]Reconnecter.exe"" ""C:\Users\Aaron T\Documents"" My.Application.Info.AssemblyName")
    You've got another problem there though. I'm guessing that you don't literally want to use "My.Application.Info.AssemblyName" as an argument. I'm guessing that you actually want to get the value contained in the My.Application.Info.AssemblyName property and use that as an argument, so that's what you should do.

    Also, I doubt you actually want to hard-code the path to the Documents folder either. It's not going to be the same on every system, is it?
    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

  21. #21

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

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    Does the file get downloaded? How long does it take? Note that if the last parameter is False and the file already exists an exception is thrown, so are you handling that exception? Is one thrown?
    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

  23. #23

  24. #24

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

    Re: Track progress of download, using My.Computer.Network.DownloadFile

    If you knew you shouldn't and did anyway, that's worse than not knowing. Do not bump your threads, plain and simple. Your posts were at 10.55 PM and 8.02 AM my time. I do have to sleep.

    I think I gave you a bum steer on that. I don't use that method at all myself and I thought that it was synchronous. I just read the documentation (as I'm sure everyone does) and I now see that it's not so, in fact, it doesn't provide notification of the download completing. It's important to remember that the My namespace exists to provide simple methods to perform common tasks. It can't do everything. If you need more than the My namespace provides then you need to fall back to standard Framework types and methods and, perhaps, do little more work yourself. In this case, that means using a WebClient. As already demonstrated, it can provide download progress and completion notification. You just have to provide the progress UI yourself.
    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

  26. #26

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