469,908 Members | 2,213 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

Launching and controlling multiple simultaneous threads with vb.net

I have a situation where I need to download multiple files in a vb.net
application.

To speed up the process, I am trying to download multiple files at one
time looping through each of the files and launching them in their own
thread (code below). The problem is that when there are hundreds of
files to download, it launches them all at the same time and half
don't get downloaded. Any idea how I could control it to launch say 4
at a time, so that when one finished another would launch until the
batch is complete? Was thinking message queues but am not sure if this
is the right path to take.

Any help is appreciated
Dim count As Integer
'loop through each file
For count = CType(txturlcountbegin.Text, Integer) To
CType(txturlcountend.Text, Integer)
' create new download object
Dim c As Common = New Common
' Hoook up the on download complete event
AddHandler c.FileDownloaded, AddressOf filesaved
'Set the file download properties
c.fn = txtFileName.Text
c.sp = txtSavePath.Text
c.wc = txtWildCard.Text
c.tb = txtBegin.Text
c.te = txtEnd.Text
c.url = Regex.Replace(txtURL.Text, txtURLWILDCARD.Text,
count.ToString)
c.batchnum = count
'launch a new thread to download the files
Dim t As New Thread(New ThreadStart(AddressOf c.getFiles))
t.Start()
Next
Nov 21 '05 #1
1 3245
Hi Ryan

Your problem is that are creating a new thread insisde
the for cylcle. I would use it to store your common
objects inside a arraylist.

Create a class with a thread counter set to zero by
default.

create a while cycle with a boolean variable.
Inside the cycle always retreive the 1st element of
arraylist and if thread counter < 5 create a new thread
with Common object and increase the counter inside your
GetFiles and remove the item from the arraylist.

Once the download as finished and filesaved event is
raised decrease the thread counter.

Once you removed the last item of arraylist terminate
while cycle.

Kind Regards
Jorge
-----Original Message-----
I have a situation where I need to download multiple files in a vb.netapplication.

To speed up the process, I am trying to download multiple files at onetime looping through each of the files and launching them in their ownthread (code below). The problem is that when there are hundreds offiles to download, it launches them all at the same time and halfdon't get downloaded. Any idea how I could control it to launch say 4at a time, so that when one finished another would launch until thebatch is complete? Was thinking message queues but am not sure if thisis the right path to take.

Any help is appreciated
Dim count As Integer
'loop through each file
For count = CType(txturlcountbegin.Text, Integer) ToCType(txturlcountend.Text, Integer)
' create new download object
Dim c As Common = New Common
' Hoook up the on download complete event
AddHandler c.FileDownloaded, AddressOf filesaved 'Set the file download properties
c.fn = txtFileName.Text
c.sp = txtSavePath.Text
c.wc = txtWildCard.Text
c.tb = txtBegin.Text
c.te = txtEnd.Text
c.url = Regex.Replace(txtURL.Text, txtURLWILDCARD.Text,count.ToString)
c.batchnum = count
'launch a new thread to download the files
Dim t As New Thread(New ThreadStart (AddressOf c.getFiles)) t.Start()
Next
.

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Quiet Man | last post: by
2 posts views Thread by Alex | last post: by
17 posts views Thread by Arjen | last post: by
2 posts views Thread by Tobiah | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.