PDA

Click to See Complete Forum and Search --> : Launching and controlling multiple simultaneous threads with vb.net


rmalone
Sep 7th, 2004, 07:22 AM
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

Cudabean
Sep 10th, 2004, 08:02 PM
This shouldn't be too hard. Just launch exactly four threads and have each thread run the same subroutine. Design the subroutine so that it reads from a global list of file names one at a time. When the subroutine can't find any more files tell it to return. Each thread will then quit automatically when all the files have been processed.

cudabean