[How-To] Asynchronous File Download with ProgressBar in VB.net
2014-02-25, 03:11 AM,
#1
If you have ever tried to download a file in VB.net, you've probably used
Code:
My.Computer.Network.DownloadFile("url","destination")
That works too but it does not output it's progress on a ProgressBar and it causes the UI to temporarily freeze.
Here's how to do it correctly:
[hide]First, if you want to use a button for the download, drag a ProgressBar and a button into the designer.
At the beginning of the code, before Public Class, add
Code:
Imports System.Net
This allows us to use the WebClient, which is what we're going to use for the download.
Now after the Public Class line, add
Code:
Dim WithEvents fileDL As New WebClient
This declares the fileDL variable as a new WebClient.
Now in the button, add
Code:
fileDL.DownloadFileTaskAsync(New Uri("DownloadURLHere"), "File.zip")
This is the line that downloads the file.
Keep in mind that you can add this in a Timer or the Form Load.
Now add the following code before the End Class line
Code:
Private Sub fileDL_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles fileDL.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
        If ProgressBar1.Value = 100 Then
            MsgBox("Done!")
            ProgressBar1.Value = 0
        End If
    End Sub
This is the code that outputs the progress in a ProgressBar, when it's done it pops up a message and then resets the ProgressBar.
Final Result Code sample:
Code:
Imports System.Net
Public Class Form1
    Dim WithEvents fileDL As New WebClient
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        fileDL.DownloadFileTaskAsync(New Uri("DownloadURLHere"), "File.zip")
    End Sub
    Private Sub fileDL_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles fileDL.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
        If ProgressBar1.Value = 100 Then
            MsgBox("Done!")
            ProgressBar1.Value = 0
        End If
    End Sub
End Class
[/hide]

"The master has failed more times than the beginner has even tried"

My VIP Shares:
How to stop Chrome from blocking PPI installers
How to make advanced, custom messageboxes that match your theme in VB.

PM ME FOR A 99% OFF DISCOUNT ON HOSTWINDS
Reply
Share This Thread :



Related Threads
Thread Author Replies Views Last Post
  OGads - How to change some words into bold in the file download locker? Lone Wolf 2 381 2018-04-20, 03:38 AM
Last Post: Lone Wolf
  Aeonsofts VB.NET Theme Manager v2.1 lanzye 31 11,932 2017-10-28, 04:48 PM
Last Post: waiphyoe
  Help needed - Download link freakkz 3 713 2017-01-29, 10:23 PM
Last Post: CharlieHarper
  help in file css vip2 1 1,014 2016-11-25, 12:36 AM
Last Post: torothemes
  .Net Expert Required insomniac 6 1,812 2016-09-08, 08:38 PM
Last Post: styx





About Us | Contact Us | CPA Elites | Advertise | Stats

© 2013-2018 CPA Elites Ltd
Enhanced by MyBB and WallBB
Return to top