Well - are you saying that that is indeed what's happening? I mean - the
loop is finishing before even the first procedure returns? From the looks of
it, I really doubt that. If you call a procedure within a loop, the loop
will not continue until the first call to the procedure returns. .NET does
not spawn of new threads in such cases. In fact, there are very rare cases
in which the framework does some stuff within the code on a different thread
(eg. Timers.Timer runs on a seperate thread). So in the case you've
mentioned, the loop will be held up till the procedure call returns and then
it will continue with the next call and so on. If you want each call to
execute on a seperate thread, you would have to do that explicitly.
Considering its a 100 iteration loop, spawning off a new thread for each
call wouldn't be such a good idea since that would be quite resource
intensive. You should instead use the ThreadPool class
(System.Threading.ThreadPool) for this. The thread pool has a limit of 25
threads. So, if you run out of all 25 of them, the execution will wait till
one thread becomes free for use. Use the QueueUserWorkItem method to queue
each procedure call and the runtime will take care of executing the call on
a thread from thread pool as soon as one is available.
Here's more info on QueueUserWorkItem:
http://msdn.microsoft.com/library/de...kitemtopic.asp
hope that helps..
Imran.
"OpticTygre" <op********@adelphia.net> wrote in message
news:LO********************@adelphia.com...
Heheh...I've got lots of questions today.
If I have a loop that calls the same subroutine several times (that
subroutine may be processor and network intensive):
For i = 1 to 100
Call mySub(IPAddress(i))
Next
And inside mySub, I'm initializing a new network connection (SFTP if you
want to know). Does this loop automatically create new threads for each
subroutine that is called? I realize the loop will not wait for one sub
to finish before it calls the next. This particular loop will be finished
before the first subroutine is even finished. What is the difference
between calling something simple like this, or writing code to initiate
new threads for each sub I'm calling? Wouldn't each subroutine be working
separate from eachother already? What would be the benefit of threading
something like this?
-Jason