Page 1 of 1

Download File Asynchronously with ProgressBar Rate Topic: -----

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 07 August 2010 - 06:38 PM

If you've ever wanted to create your own Download Manager, where you download a file, keep track of the amount of the file that has been downloaded, and use a ProgressBar, this is the tutorial for you.

So first, drag a ProgressBar and a Button from the Toolbox onto the form.

Create a Click_Event for the button by double clicking it.

Add this using statement to the top of the form.

Imports System.Net



In the Click_Event, we are going to create a WebClient object to download the file.

    Private Sub btnStartDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartDownload.Click
        Dim client As WebClient = New WebClient
        AddHandler client.DownloadProgressChanged, AddressOf client_ProgressChanged
        AddHandler client.DownloadFileCompleted, AddressOf client_DownloadCompleted
        client.DownloadFileAsync(New Uri("SomeURL"), "SomePlaceOnHardDrive")
        btnStartDownload.Text = "Download in Progress"
        btnStartDownload.Enabled = False
    End Sub



In the previous code, we are creating two event handlers to handle when the Progress of the file download changes, and when the download completes.

We also use the DownloadFileAsync method so that the download(which could be lengthy) does not freeze the main GUI.

Next, we need to do the code to calculate the percentage of the download complete so that we can assign it to the ProgressBar.

    Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
        Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
        Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
        Dim percentage As Double = bytesIn / totalBytes * 100

        progressBar.Value = Int32.Parse(Math.Truncate(percentage).ToString())
    End Sub



In this code, we use the BytesReceived and TotalBytesToReceive properties of the DownloadProgressChangedEventArgs object.

Next, we need to do the code for the event when the download completes.

    Private Sub client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        MessageBox.Show("Download Complete")
        btnStartDownload.Text = "Start Download"
        btnStartDownload.Enabled = True
    End Sub



If you would like to have the percentage "embedded" into the ProgressBar, check out JacobJordan's tutorial here, http://www.dreaminco...wtopic94631.htm

Is This A Good Question/Topic? 1
  • +

Replies To: Download File Asynchronously with ProgressBar

#2 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

Posted 17 August 2010 - 11:48 AM

thanks for the tutorial :P
Was This Post Helpful? 0
  • +
  • -

#3 MistGun  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 27-October 10

Posted 28 October 2010 - 02:33 PM

Nice tutorial bro! thanks :D
Was This Post Helpful? 0
  • +
  • -

#4 B7vks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 02-July 12

Posted 13 January 2013 - 02:03 AM

nice
Was This Post Helpful? 0
  • +
  • -

#5 kmanios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-February 13

Posted 19 February 2013 - 03:54 AM

Hello,

I just tried this tutorial in VS2012 and I keep getting the following exception:

mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Delegate.DynamicInvokeImpl(object[] args)
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbackDo(System.Windows.Forms.Control.ThreadMethodEntry tme)
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(object obj)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallback(System.Windows.Forms.Control.ThreadMethodEntry tme)
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbacks()
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativewindow.OnMessage(ref System.Windows.Forms.Message m)
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativewindow.WndProc(ref System.Windows.Forms.Message m)
System.Windows.Forms.dll!System.Windows.Forms.Nativewindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData)
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context)
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context)
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext context)
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(string[] commandLine)
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args)
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
[Native to Managed Transition]



This always happens right when the download is completed (I see the progress bar going up to 95% and then get this). The download is indeed completed successfully because I see the file in my hard drive.

Any clues as to why this may happen?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1