By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,345 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,345 IT Pros & Developers. It's quick & easy.

Launching and controlling multiple simultaneous threads with vb.net

P: n/a
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
Share this Question
Share on Google+
1 Reply

P: n/a
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.