# VBForums CodeBank > CodeBank - Visual Basic .NET >  Asynchronous File Downloading Queue System

## formlesstree4

Update (1/16/2010): Rewrote the entire thing cause I wasn't satisfied with the use of Queue. I had no real control over it and couldn't really do much other than dequeue, enqueue, and peek at the list. So I changed everything. I also fully commented it this time, and ended up creating a Structure for the Download Items.

Update (3/3/2010): Added in a few new items here and there. Included Transfer Rate calculations (in kilobytes per second, rounded to two decimal places). The code is attached because it's to long for this post.

Update (3/19/2010): Fixed the transfer rate calculations so they actually work properly. Added in a few properties to easily remove items and some more events so you know when a file is added.

Update (5/13/2010): Applied DJ PIP's fix to the speed calculations (you can see his post down below if you don't want to redownload the file).

Update (7/18/2010): Adding the beta ASQS to this post now. I did a major rework on the events, so it will break existing implementations of this class (sorry djpip). I changed them to look similar to the framework (ByVal sender As Object, ByVal e As someargtype), so all data is now grouped in a single structure for each event rather than a long signature for each event. This also means you can Override three new subs:
OnProgressChange, OnFileCompletion, and OnFileError. I do plan to add in more events as I work on this guy some more to flush him out and give him a few more features. I also did a few bug fixes here and there as well as some code changes.

Update (10/21/2010): Added a trashy Demo App that I'm gonna work on some more. Threw it together real fast due to get an example out and cause I'm low on time.

Questions, comments, and criticism are welcome.

----------


## formlesstree4

The 2nd update brings a bit more to the table, with the ability for it to calculate the transfer rate and pass it through. It updates literally every time the progress changes, so it's accuracy is quite spot on, but because it updates so often, it's not always consistent (like how Firefox/google chrome have more consistent ones). It still gets the job done however.

More functionality is being worked on so it's much easier to stop and skip downloads (not pause because the WebClient doesn't have a pause method, and I don't know how I'll get one to work.)

----------


## formlesstree4

Did a 3rd update today, let me know how things work out. (changes in first post).

----------


## djpip27

Hi there,

First of all can I say a big thank you.
I too am using a webclient in my software which downloads multiple files from the internet. Your class is clearly organised, easy to use and I can't wait to get it working fully in my program!

I am having a little trouble with the Percentage Event.
Basically, this is a really daft question  :Wink:  but ...

How can I get the percentage values?
I created this sub in my program to handle the event, but how do I get the data from it?

Public Sub ProgressChange() Handles downloadersystem.Percentage

End Sub

Many Thanks.
DJ PIP

----------


## djpip27

Don't worry.
Figured it out! 

Straightforward.

Public Sub ProgressChange(ByVal currentFile As String, ByVal prog As Integer, ByVal Rate As String) Handles downloadersystem.Percentage

    End Sub

Thank you.
DJ PIP

----------


## formlesstree4

Glad you figured it out! If any problems arise, let me know.

----------


## djpip27

*Fix for Download Speeds.*

Amazing Class as I said earlier.
Got it working really well in my software.

I've noticed when downloading the speed will suddenly jump after around 60 seconds to several GB's per second. I'd love this to be true, but unfortunately I think this is caused by a small error in your code.


In this section ...
Private Sub ChangeInProgress(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)

I believe this should be the correct formula to calculate the speed.

Speed = CInt((CurBytes / SpeedCalculate.Elapsed.*TotalSeconds*))


This is because when using ...
Speed = CInt((CurBytes / SpeedCalculate.Elapsed.*Seconds*))
... after 59 seconds, it then resets to 01, 02, 03 again, when actually the download is still happening.

See what you think.

DJ PIP

----------


## formlesstree4

Thank you for noticing that. I hadn't had a download last that long and I never really figured it would do that to me. If it's working for you and you haven't had an issue, I'll gladly fix my code.

----------


## djpip27

Hi,

Just wanted to update this thread slightly.
I've been using your code for a month now, works perfectly and I have experienced no problems after making that small change I suggested earlier so that download speeds are more accurate.

Downloads have been working wonderfully and I can't thank you enough.

Thank you for your hard work!

DJ PIP

----------


## macbrutal

Hi, Sorry to bother you guys but I can't understand how to use this module!
I would love a tiny explaination for this one like, what do I have to do to make those functions work?
I tried to call a few functions but that just gave me "It's not declared" etc.

I've been reading about the basics about modules in VB.net and all I could found was like this:
-------------
(Module)
Public sub example()
...code...
(Form1.vb)
Call example
--------------

----------


## formlesstree4

What's wrong exactly? This isn't a module, it's a class, so things are done differently. You need to declare a New Class, and call the variables from that.

Example:



```
Dim x As New DS.ASQS
x.AddToQueue("url", "savelocation")
```

----------


## formlesstree4

Another update, hopefully the final one. The update uses DJ PIP's fix for the Download Speeds (thanks again for spotting that), so I hope the class is virtually bug free now.

----------


## formlesstree4

This is a beta of a rewrite I'm doing.

Basically, it allows for simultaneous downloads rather than just one at a time. I have yet to see how it actually will flow, and I'm thinking it will give me problems.

I would like to have some beta testers give it a spin since I can't really easily test it right now. It's attached (Visual Studio 2010 project), and if you all can't compile it due to using 2008 or some other reason, let me know and I'll send you a compiled version.

Update 1 (May 20th, 2010): Added in 3 lines to enable the missing handlers. Helps if all events are properly raised.  :Smilie: 

Update 2 (May 22nd, 2010): Added some new stuff in. If you reduce the maximum downloads below your current downloads, it starts stopping the latest downloads, nice little thing. Few other misc. bugfixes, things of that nature.

Update 3 (July 18, 2010): See main post for new attachment and update notes.

----------


## djpip27

I have placed this new code in my software.
The downloads themselves work and I can get the simultaneous downloads working as designed.
Unfortunately I can't seem to get the events such as progress changed to work.

I'm using it like this ...

Public WithEvents downloadersystem As New ManagerClass
...
'I add some downloads, start them and then use this code to report the progress.

Public Sub ProgressChange(ByVal file As String, ByVal speed As String, ByVal percent As Integer) Handles downloadersystem.ProgressChanged

When I debug my application, after placing a breakpoint at ProgressChange, the breakpoint is marked stating that it will never be executed.
Am I doing this in the right way?

DJ PIP

P.S: It looks fantastic so far, and I can't wait until we get this fully working.

----------


## formlesstree4

OK, I found the error.

In ManagerClass.vb, find the Add() Sub. Replace it with this:



```
    Public Sub Add(ByVal FD As FileData)
        Dim DC As New DownloadClass(FD)
        If Not CheckForItemInList(FD) Then
            DownloadList.Add(DC)
            AddHandler DC.FileError, AddressOf FileErrorSub
            AddHandler DC.ProgressChanged, AddressOf ProgressChangedSub
            AddHandler DC.ProgressFinished, AddressOf ProgressFinishedSub
            If AutoDownload Then RunList()
        End If
    End Sub
```

I'll put an updated zip in the post of the beta, including a change log.

----------


## djpip27

Great getting the progress of downloads works fine now.
One other small thing I'd noticed.
Some-times the download complete event fires twice.

Don't know why, but I'd noticed this when using the webclient before.
There's nothing you can do about this I don't think, although a small change here....

Download Class.

 Private Sub WC_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
        If e.Error IsNot Nothing Then
            RaiseEvent FileError(Data.FileName, e.Error)
        End If
        RaiseEvent ProgressFinished(Data.FileName)
        If SpeedCalculate IsNot Nothing Then
            SpeedCalculate.Stop()
            SpeedCalculate = Nothing
        End If
        GC.Collect()
    End Sub

If SpeedCalculate IsNot Nothing Then ... Stops a Null-Reference exception.

Will integrate it some more into my Application and will report back soon.

DJ PIP

----------


## djpip27

I'm loving the new system. Really nice work.  :Big Grin: 

I have a question.
I'm currently allowing 5 downloads at a time, and sometimes adding about 8/9 downloads into the list and then starting it.
After a download finishes, another slot of my original 5 is now 'free'.
How can I make it download the next one in the list, in this case number 6?

I hope this makes some sense.
Eg. Downloads 1-5 start.
     Downloads 6-8 are paused (max limit of 5 reached.)
     Download 1 finished.
     Download 6 can start.
     Download 4 finishes.
     Download 7 can start.

----------


## formlesstree4

For some reason, I forgot to add a line of code in the ProgressFinishedSub. Whenever the event is raised, at least for now, call RunList() to start the next download, or you can edit the ProgressFinishedSub yourself (it's in the ManagerClass). Just add "Call RunList()" at the end of it:


```
    Private Sub ProgressFinishedSub(ByVal file As String)
        Dim DC As DownloadClass = FindDownloader(file)
        Call Remove(file)

        RemoveHandler DC.ProgressChanged, AddressOf ProgressChangedSub
        RemoveHandler DC.FileError, AddressOf FileErrorSub
        RemoveHandler DC.ProgressFinished, AddressOf ProgressFinishedSub

        RaiseEvent ProgressFinished(file)
        DC.Dispose()

        Call RunList()

    End Sub
```

----------


## djpip27

Great Work.
You also need to add 'CurrentDownloads -= 1' before you Call RunList().
If you don't do this then the CurrentDownloads value remains at your maximum and then no more start.

DJ PIP

----------


## formlesstree4

Ah yes, I forgot that too. Thanks for the help. Hopefully it won't cause many issues and should be relatively bug free (now). I'll upload a fixed zip file later tonight.

----------


## formlesstree4

Updated to fix a few other bugs I had missed when I originally coded the system.

----------


## Grebo

Hello,

I would like to use this class for a project I have that is being converted to .NET 2010.  I compiled the ASQS class and made reference to the dll in a new project but am having trouble with the first step in adding a download.  The .data property from downloadclass isn't listing the saveto, URL, etc. I'm new to .NET and must be something I'm missing.. 



```
Imports ASQS
Imports ASQS.FileData
Imports System

Public Class Form1
    Public WithEvents dclass As ASQS.DownloadClass
    Public WithEvents s As New ASQS.ManagerClass
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wmp3 As New ASQS.FileData
        wmp3.Equals?
        dclass.Data.Equals?

    End Sub
End Class
```

Thanks

Grebo

----------


## formlesstree4

Open up the FileStructure.vb file and all the Properties from "Friend" to "Public", then recompile the DLL.

----------


## Grebo

Thank you, but I am now having a problem starting a download.  I had to make changes to WC_DownloadFileCompleted in the DownloadClass just like djpip27 did so it wouldn't give null object reference error. I get no error in the code now and no errors while running the compiled application/clicking Button1 but nothing is getting downloaded.

I'm not sure if I am adding a download correct..



```
Imports System
Imports ASQS
Imports ASQS.FileData

Public Class Form1
    Public WithEvents dclass As DownloadClass
    Public WithEvents s As New ManagerClass

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim df As New ASQS.FileData
        's.AutoDownload = True
        s.MaxDownloads = 2
        df.SaveTo = "C:\Downloads"
        df.URL = "http://www.site.com/song.mp3"
        s.Add(df)
        s.RunList()
    End Sub
End Class
```

Thanks

Grebo

----------


## formlesstree4

What OS are you running and do you have permissions to download to the location as well as retrieve it from the website?

----------


## Grebo

I'm running Windows XP and yes I've downloaded the same files just fine using the just the QueueSystem.vb class.

----------


## formlesstree4

Try stepping through the code and make sure that no exceptions are being thrown but ignored by the code, it's happened before. I might have placed a careless Try Catch without actually handling the error.

----------


## Grebo

Wow, I forgot to actually set a file to save as in the saveto, should be df.SaveTo = "C:\Downloads\thesong.mp3".  Everything works fine, thank you for the downloading class.  If you are wondering, I'm making a mp3 ripper.

----------


## formlesstree4

Well, glad you figured it out  :Smilie:

----------


## vixez

I can't quite figure out how to use this properly.

I have this code:



```
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim df As New FileData
            's.AutoDownload = True
            s.MaxDownloads = 2
            df.SaveTo = "D:\Users\Glenn Ruysschaert\Documents\Visual Studio 2008\Download\t.txt"
            df.URL = "http://badboy70.cwahi.net/welcome.txt"
            s.Add(df)
            s.RunList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
      
    End Sub

    Private Sub dclass_ProgressChanged(ByVal file As String, ByVal speed As String, ByVal percent As Integer) Handles dclass.ProgressChanged
        Label1.Text = percent
    End Sub

    Private Sub dclass_ProgressFinished(ByVal file As String) Handles dclass.ProgressFinished
        MsgBox("done")
    End Sub
```

It downloads the file, but the label doesn't change and the msgbox doesn't show up either.

What am I doing wrong?

----------


## formlesstree4

I'm surprised you're not getting some sort of cross threading error. Those events are raised on a different thread so you need to use delegates to bring them back over.

----------


## JuggaloBrotha

Hey formless, I have a suggestion:  When you raise events, give them the normal signature with sender being the object that raised it and e having the properties of the other information.  So instead of:

```
Private Sub dclass_ProgressChanged(ByVal file As String, ByVal speed As String, ByVal percent As Integer) Handles dclass.ProgressChanged
```

You have:

```
Private Sub dclass_ProgressChanged(ByVal sender As Object, ByVal e As ProgressArgs) Handles dclass.ProgressChanged
```

where e has these properties of your ProgressArgs class: file As String, speed As String, percent As Integer

Also I would make percent a Double (between 0.0 and 1.0) instead of an Integer.

----------


## formlesstree4

I'll try doing that on the next update, thanks for the tip.

----------


## Grebo

I'm having trouble as well with trying to set a handler for progresschanged event in the download class.  I've been trying to follow tutorials on this but no luck and here what I have but it is throwing an error on the AddHandler dclass.ProgressChanged, AddressOf OnProgress line with this "Object reference not set to an instance of an object." 



```
Option Explicit On
Option Strict On

Imports System
Imports ASQS
Imports ASQS.FileData
Imports ASQS.DownloadClass
Imports ASQS.ManagerClass
Public Class Form1
    Public WithEvents dclass As DownloadClass
    Public WithEvents sas As New ManagerClass

    Public Delegate Sub ProgressChangedEventHandler(ByVal file As String, ByVal speed As String, ByVal percent As Integer)

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim where As New FileData
        AddHandler dclass.ProgressChanged, AddressOf OnProgress

        sas.AutoDownload = True
        sas.MaxDownloads = 2
        where.SaveTo = "C:\Downloads\test.mp3"
        where.URL = "http://www.site.com/a.mp3"
        'MsgBox(where.FileName)
        'MsgBox(where.FilePath)
        sas.Add(where)
        'sas.RunList()
    End Sub
    Public Sub OnProgress(ByVal file As String, ByVal speed As String, ByVal percent As Integer) Handles dclass.ProgressChanged

        If Me.InvokeRequired Then
            Me.Invoke(New ProgressChangedEventHandler( _
            AddressOf OnProgress _
            ), New Object() {percent})
        Else
            Me.ProgressBar1.Value = percent
        End If
    End Sub
End Class
```

Here is the detailed exception:


```
System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=pmr
  StackTrace:
       at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\pmr\pmr\Form1.vb:line 17
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
```

Thanks

----------


## formlesstree4

Change


```
Public WithEvents dclass As DownloadClass
```

to


```
Public WithEvents dclass As New DownloadClass
```

----------


## Grebo

I've been trying to figure out why I couldn't do that as well because when I try it throws this error:
Overload resolution failed because no accessible 'New' accepts this number of arguments.

----------


## formlesstree4

Well, what does it want from you? Did you read the Intellisense? I commented on the entire class pretty much with XML comments.

----------


## Grebo

d
I set it as Public WithEvents dclass As New DownloadClass(vbNullString, vbNullString) or 
    Public asdf As FileData
    Public WithEvents dclass As New DownloadClass(asdf)

which it seems to accept but I can't actually use the downloadclass because I tried doing an dclass.abort and dclass.dispose but nothing happens trying both.  I'm assuming that is why the ProgressChanged event handler isn't working as well..

----------


## formlesstree4

I just realized something, you only should be using the ManagerClass. It is the one that controls all the stuff on the system. I'm sorry about not remembering sooner, it's been a long day.


Try Handling the ManagerClass events instead.

----------


## Grebo

Can someone please tell me what I'm missing in getting the event to actually fire using delegates..

This is what I have.



```
Option Explicit On
Option Strict On

Imports System
Imports ASQS
Imports ASQS.FileData
Imports ASQS.DownloadClass
Imports ASQS.ManagerClass
Public Class Form1

    Public WithEvents sas As New ManagerClass
    Public EventInvoker As prog_del
    Public Delegate Sub prog_del(ByVal file As String, ByVal speed As String, ByVal percent As Integer)

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim where As New FileData

            sas.AutoDownload = True
            sas.MaxDownloads = 2
            where.SaveTo = "C:\Downloads\test.mp3"
            where.URL = "https://www.site.com/asd.mp3"
            'MsgBox(where.FileName)
            'MsgBox(where.FilePath)

            sas.Add(where)
            'sas.RunList()
        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)
        End Try

    End Sub

    Private Sub sas_DownloadProgressChanged(ByVal file As String, ByVal speed As String, ByVal percent As Integer) Handles sas.ProgressChanged
        If Me.Label1.InvokeRequired Then
            EventInvoker = New prog_del(AddressOf Me.sas_DownloadProgressChanged)
            Me.Label1.Invoke(EventInvoker, speed)
            'Exit Sub
        End If
        Me.Label1.Text = speed
    End Sub
End Class
```

----------


## formlesstree4

So...what happens? Is an error thrown? I'm assuming from what you've said, nothing happens.

----------


## Grebo

Yeah, nothing happens and no errors are thrown.

----------


## formlesstree4

Try doing this...



```
Private Delegate Sub d_ChangeLabel(ByVal text As String)
Private Sub ChangeLabel(ByVal text As String)
    If Label1.InvokeRequired Then
        Label1.Invoke(New d_ChangeLabel(AddressOf ChangeLabel), text)
    Else
        Label1.Text = text
    End If
End Sub
 
    Private Sub sas_DownloadProgressChanged(ByVal file As String, ByVal speed As String, ByVal percent As Integer) Handles sas.ProgressChanged
        Call ChangeLabel(speed)
    End Sub
```

----------


## Grebo

I just tried that and nothing still.  I'm just making a reference to the ASQS.dll trying this and was wondering if I'm suppose to add the actual ASQS project(downloadclass.vb, filestructure.vb..) to this project and delete the reference?

----------


## formlesstree4

Did you try stepping through it to see if things were actually being processed? Try calling the Runlist() method anyway, just in case setting AutoDownload to true didn't work.

----------


## Grebo

I tried calling just the runlist() instead and still nothing.  I tried stepping through and end up at this inside downloadclass.vb with it only stepping through

        Dim CurBytes As Long = e.BytesReceived
        Dim StreamEnd As Long = e.TotalBytesToReceive
        If SpeedCalculate.Elapsed.TotalSeconds > 0 Then

and then jumping to 

End

The download finishes but if I keep stepping through it repeats the above jumping.  Is it suppose to jump from  If SpeedCalculate.Elapsed.TotalSeconds > 0 Then to End while a file is downloading or is there something wrong with the speedcalculate stopwatch?

----------


## formlesstree4

How big is the file you're downloading?

----------


## Grebo

It is 6,420 KB and an mp3 file.

----------


## formlesstree4

The server you're getting it from, does it allow files to be downloaded with out credentials?

----------


## Grebo

I have no idea but I just tried again on a different file which is some indie game I found that is bigger in size(19,744 kb) for testing and still nothing.

EDIT: here is the game download link, http://www.nomoresweden.com/files/manenough.zip  , are you sure the SpeedCalculate.Elapsed.TotalSeconds has nothing to do with the progress not being shown?

EDIT: I also added a watch to SpeedCalculate.Elapsed.TotalSeconds and it stays 0.0 for the value while the file is downloading..

EDIT: I added this to the DownloadClass.vb and compiled a new ASQS.dll.  I'm not sure if that is exactly where I should start the SpeedCalculate though, but the progress change event worked for getting speed.  I'll have to try another step through I guess..



```
    Public Sub Start()
        If Not Downloading Then
            WC.DownloadFileAsync(New Uri(Data.URL), Data.SaveTo)
            Downloading = True
            SpeedCalculate.Start() 'added this line
        End If
    End Sub
```

----------


## formlesstree4

I'm quite surprised I missed that. I'll look over the code (I'm doing a rewrite as I type this) to make sure that's where that should go. If so, thanks for pointing this out and I'm surprised that this hasn't caused an issue beforehand.

----------


## formlesstree4

Excuse the double post, but I have released a new version that has some moderate changes and with more to come.

----------


## Grebo

How would I handle the ProgressFinished event for example with this new event patterns change and what are the advantages?  Thanks

EDIT: Do you know how or what would be easiest method for determining which file has has finished downloading from a listview item using this class.  I'm been wondering how exactly I am going to do this because I'll be tagging each mp3 when they are finished downloading but seems like it going to a very complicated process.

----------


## formlesstree4

The advantages is that instead of one long signature, you get a signature that's two objects max, the 1st being the sender, and the 2nd being an organized structure that contains all the information you need.

As for your 2nd question, override the OnFileCompletion() sub, you can do your tagging in there if the file is an MP3.

----------


## Grebo

Sorry for so many questions but the switch hasn't been fun.  I am not sure if I'm actually doing this correctly but the msgbox shows twice and I get an error while debugging with the error "Object reference not set to an instance of an object." on this line SpeedCalculate.Stop() inside Private Sub WC_DownloadFileCompleted (DownLoadClass.vb) . I'm not sure also on how to get file, speed and percent correctly with the changes.  Thanks



```
Imports ASQS
Public Class Form1
    Public WithEvents asdf As New ManagerClass
    Public Delegate Sub prog_event(ByVal sender As Object, ByVal e As ASQS.ManagerClass.ProgressEventArgs)

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim test As FileData

        asdf.AutoDownload = True
        'asdf.MaxDownloads = 2

        test.SaveTo = "C:\Downloads\test.mp3"
        test.URL = "http://www.site.com/themp3.mp3"
        'MsgBox(test.FileName)
        'MsgBox(test.FilePath)

        asdf.Add(test)
        AddHandler asdf.ProgressFinished, AddressOf OnMyEvent
    End Sub
    Public Sub OnMyEvent(ByVal sender As Object, ByVal e As ASQS.ManagerClass.FileCompletionArgs) Handles asdf.ProgressFinished
        RemoveHandler asdf.ProgressFinished, AddressOf OnMyEvent
        MsgBox("done")
    End Sub
    Public Sub OnMyEvent2(ByVal sender As Object, ByVal e As ASQS.ManagerClass.ProgressEventArgs) Handles asdf.ProgressChanged
        Try
            If Me.InvokeRequired Then
                Me.Invoke(New prog_event( _
                AddressOf OnMyEvent2 _
                ), New Object() {percent}, {speed})
            Else
                Me.ProgressBar1.Value = percent
                Me.Label1.Text = speed
            End If
        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)
        End Try
    End Sub
End Class
```

----------


## Grebo

I'm still not quite sure how I am suppose to match all the tags/picture.jpg with the mp3 that has finished downloading because if I am going with the multiple download option I could get a third mp3 file finishing before the first and so on..

Never mind about not being able to get speed, file and filename correctly, I figured it out and it is easier than I thought. 



```
    Public Sub OnMyEvent(ByVal sender As Object, ByVal e As ASQS.ManagerClass.FileCompletionArgs) Handles asdf.ProgressFinished
        MsgBox(e.FileName)
    End Sub
```

 I am though getting an error still inside this,



```
    Private Sub WC_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
        If e.Error IsNot Nothing Then
            RaiseEvent FileError(Data.FileName, e.Error)
        Else
            RaiseEvent ProgressFinished(Data.FileName)
        End If
        Downloading = False
        SpeedCalculate.Stop() 'this line is highlighted with error dialog
        GC.Collect()
    End Sub
```

and I believe it could be avoided using something like this? ..



```
    Private Sub WC_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
        If e.Error IsNot Nothing Then
            RaiseEvent FileError(Data.FileName, e.Error)
        End If
        Downloading = False
        RaiseEvent ProgressFinished(Data.FileName)
        If SpeedCalculate IsNot Nothing Then
            SpeedCalculate.Stop()
            SpeedCalculate = Nothing
        End If
        GC.Collect()
    End Sub
```

----------


## formlesstree4

You can do that work around, I'm not sure why it's still throwing that though...I'll investigate it more. Thank you for the feedback and sorry that I didn't reply to your previous one. I have been a bit busy recently with other things.

----------


## Grebo

Formlesstree4, can you give me example on how I could populate a listview with the filename, percentage, and speed for each download in the currentdownlist?  I'm going to use a dictionary to store data for tagging with the filename being the key to look for in the dictionary when the file has finished downloading for tagging. Thanks,
Grebo

----------


## formlesstree4

Perhaps when I fully finish with my class, I can do that. I'm still working on tweaking things here and there, adding new events and what not.

----------


## JohnDorian

this is great wrk formlesstree.

im new to vb and i dont know how to get this to work. can you show me a example?

i have 5 files i want to download and i want to display a progressbat. i added the queuesystem.vb file to my project and i imported the namespace, but i don't know how to use the class. it would be very helpful if you could add an example to your first post.

thx

----------


## formlesstree4

So you're using the original version, not the multiple downloading version, correct? As I said in post #59, I'll get some examples up eventually. If someone else were to write examples using the class, that's fine too, I'm just to busy right now to get an example application running.

----------


## JohnDorian

> So you're using the original version, not the multiple downloading version, correct? As I said in post #59, I'll get some examples up eventually. If someone else were to write examples using the class, that's fine too, I'm just to busy right now to get an example application running.


i think i'm using the multiple downloading version. at least i want to.

what i'm asking i thought should be too hard to make. i just need to add urls to queue and then download w/ progressbar.

----------


## formlesstree4

> i think i'm using the multiple downloading version. at least i want to.
> 
> what i'm asking i thought should be too hard to make. i just need to add urls to queue and then download w/ progressbar.


It's not, the class isn't difficult to use. The version you're using, if you just downloaded the .VB file, is one that queues files and downloads them one at a time rather than simultaneously. Just create a new instance of the class and Add items to the queue. If you read the .VB file, you'll understand exactly what to do as I'm pretty sure I commented everything there.

----------


## JohnDorian

> It's not, the class isn't difficult to use. The version you're using, if you just downloaded the .VB file, is one that queues files and downloads them one at a time rather than simultaneously. Just create a new instance of the class and Add items to the queue. If you read the .VB file, you'll understand exactly what to do as I'm pretty sure I commented everything there.


But what about the progressbar?

i did what you said and i had no problem with downloading, but i dont know how to get progress. i look into the class comments like you suggested.

----------


## formlesstree4

> But what about the progressbar?
> 
> i did what you said and i had no problem with downloading, but i dont know how to get progress. i look into the class comments like you suggested.


There is a specific event that is raised which gives the progress for the file currently being downloaded. I'm not sure which one it is right now..but it is in there.

----------


## Xoslize

*I'm sorry if this is too old to be bumped, I'm kind of new.*

I'm using the Demo to test out how it works. When I add the download to queue, Nothing happens. Stays the same at 0% precent. Nothing.

----------


## formlesstree4

> *I'm sorry if this is too old to be bumped, I'm kind of new.*
> 
> I'm using the Demo to test out how it works. When I add the download to queue, Nothing happens. Stays the same at 0% precent. Nothing.


What's the file you're trying to download?

----------


## Xoslize

> What's the file you're trying to download?


A direct link, a rar file. Anything I tried doesn'ts eem to wrok.

----------


## formlesstree4

> A direct link, a rar file. Anything I tried doesn'ts eem to wrok.


I can't remember if you're supposed to start it or if it downloads automatically...I'll look into it when I get some time.

----------


## Xoslize

> I can't remember if you're supposed to start it or if it downloads automatically...I'll look into it when I get some time.


What's the code to start downloading all the queue? Because your code to add it to the queue and the checkbox to auto download is missing code.

----------


## batori

Great work there!

Nice class, just downloaded the project and checking it out. 

What i would suggest is to add Remaining Time, Downloaded so far and Time remaining columns.

----------

