0

I am trying to make a download manager for my program. But when I run this code it gives me the message box say "Download Started" but that is it. I do not get any file downloaded or progress bar change? Does anyone know why?

Public Class frmDownloader

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebClient1.DownloadFileAsync(New Uri("https://s3.amazonaws.com/MinecraftDownload/launcher/Minecraft_Server.exe"), "C:\hi.exe")
        MsgBox("download started")
    End Sub

    Private Sub WebClient1_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles WebClient1.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
        MsgBox("Download Progress Changed")
    End Sub


End Class
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Kuzon
  • 782
  • 5
  • 20
  • 49
  • Related thread - http://stackoverflow.com/questions/2826190/does-webclient-openfileasync-fire-downloadprogresschanged – KV Prajapati Dec 11 '11 at 10:08

1 Answers1

2

The DownloadProgressChanged method is invoked on a different thread than the one that started the download. Inside this callback you seem to be manipulating some GUI element: ProgressBar1.Value. You should never manipulate GUI elements on different threads than the one on which they were created or you might get an exception. Depending on the type of application you are working on there are different ways to marshal calls on the GUI thread. For example in WinForms you should use the Control.BeginInvoke method. In WPF and Silverlight the equivalent is the Dispatcher.BeginInvoke.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for the responice... But I don't understand sorry. Can you please give an example. :) Thanks. I am working just on a normal Form Project I need just to download an .jar file for my game server configuring program – Kuzon Dec 11 '11 at 10:14
  • 2
    @Kuzon, if you are doing WinForms/WPF development you should definitely read about this issue. Here's a nice blog post which explains in details: http://weblogs.asp.net/justin_rogers/pages/126345.aspx – Darin Dimitrov Dec 11 '11 at 10:16
  • Oh man... This is a touch above my level XD I am a self taught 14 year old... I am really struggling with that, I guess I will just drop that idea for the moment, I just can't get my head around this atm :( Thanks for the help... I don't even know what WinForms/WPF development means... All I know is that I go into VB.net and click form application... I am so confused XD – Kuzon Dec 11 '11 at 10:25