473,503 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading and loops with subroutines

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
Nov 21 '05 #1
5 1518
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

Nov 21 '05 #2
It's interesting you say that, Imran. If I put a messagebox.show after that
loop, and one at the end of the subroutine the loop calls, the messagebox
right after execution of the loop is the one that finishes first. That
being the case, it is apparent to me that "Calling a subroutine" will not
halt process on the main form to wait for the subroutine to finish. BTW,
the subroutine that I'm actually calling is in it's own class, and the
subroutine is being called from a module.

-Jason
"Imran Koradia" <no****@microsoft.com> wrote in message
news:eC**************@TK2MSFTNGP14.phx.gbl...
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


Nov 21 '05 #3
"OpticTygre" <op********@adelphia.net> wrote in message
news:hq********************@adelphia.com...
It's interesting you say that, Imran. If I put a messagebox.show after
that loop, and one at the end of the subroutine the loop calls, the
messagebox right after execution of the loop is the one that finishes
first.
I don't see how that's possible. There's just one thread executing here and
that's the one going through your loop. Unless you're doing some sort of
asnyc operation or spawning a new thread or the likes in a way that'll cause
the method call to return, I don't see this happening. Maybe if you post the
code of your method it would be more clear.
That being the case, it is apparent to me that "Calling a subroutine" will
not halt process on the main form to wait for the subroutine to finish.
That is incorrect. In a standard windows application (without new threads
and all), you will only have the main UI thread and whenever you call a
subroutine, everything will halt till the procedure completes execution (or
you exit out of it explicitly or there's an exception) which is why you see
so many people complaining about their form freezing up when executing a
long running process ! (search this group for several such messages..)
BTW, the subroutine that I'm actually calling is in it's own class, and
the subroutine is being called from a module.


I'm pretty sure there's some multi-threading or asnyc operation going on
which is why you are seeing such behaviour. If you don't mind posting the
code, it would probably clear up things.
hope that helps..
Imran.
Nov 21 '05 #4
I would post the code, except the fact it is for gov't work. Also, you may
be right about the async operation. I'm actually calling a subroutine
inside of a class which itself spawns new threads (I think). It uses an
external dll, and most likely that's what's happening. Thanks for the info
on threading. I've actually never used it, and just like everyone else, for
quick results I've just used Application.DoEvents() to keep the program from
freezing. heheh. I know. It's the lazy way out, but with deadlines, what
can you do? Especially when I don't know anything about threading.
Definately something I will look into, though. Thanks again.

-Jason

"Imran Koradia" <no****@microsoft.com> wrote in message
news:Ow****************@tk2msftngp13.phx.gbl...
"OpticTygre" <op********@adelphia.net> wrote in message
news:hq********************@adelphia.com...
It's interesting you say that, Imran. If I put a messagebox.show after
that loop, and one at the end of the subroutine the loop calls, the
messagebox right after execution of the loop is the one that finishes
first.


I don't see how that's possible. There's just one thread executing here
and that's the one going through your loop. Unless you're doing some sort
of asnyc operation or spawning a new thread or the likes in a way that'll
cause the method call to return, I don't see this happening. Maybe if you
post the code of your method it would be more clear.
That being the case, it is apparent to me that "Calling a subroutine"
will not halt process on the main form to wait for the subroutine to
finish.


That is incorrect. In a standard windows application (without new threads
and all), you will only have the main UI thread and whenever you call a
subroutine, everything will halt till the procedure completes execution
(or you exit out of it explicitly or there's an exception) which is why
you see so many people complaining about their form freezing up when
executing a long running process ! (search this group for several such
messages..)
BTW, the subroutine that I'm actually calling is in it's own class, and
the subroutine is being called from a module.


I'm pretty sure there's some multi-threading or asnyc operation going on
which is why you are seeing such behaviour. If you don't mind posting the
code, it would probably clear up things.
hope that helps..
Imran.

Nov 21 '05 #5
"OpticTygre" <op********@adelphia.net> wrote in message
news:a-********************@adelphia.com...
I would post the code, except the fact it is for gov't work. Well - I would have loved to see what top secret code gov't folks write..
;-) just kidding..
Also, you may be right about the async operation. I'm actually calling a
subroutine inside of a class which itself spawns new threads (I think). It
uses an external dll, and most likely that's what's happening. Thanks for
the info on threading. hmm..I suspected that...
I've actually never used it, and just like everyone else, for quick
results I've just used Application.DoEvents() to keep the program from
freezing. heheh. I know. It's the lazy way out, but with deadlines,
what can you do? Especially when I don't know anything about threading.
Definately something I will look into, though. Same here - I'd use DoEvents picking up on the old VB6 habits and if a piece
of code ain't broke, don't change it :) Apparently, threading is something
people choose as a last resort since not getting it right (which in itself
is a pain) can lead to subtle bugs that are more often than not a pain in
the neck to debug. It needs a little more practice than other stuff, that's
all.
Thanks again.

you're welcome !
Imran.
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
1615
by: chahnaz.ourzikene | last post by:
Hi all, This is the first i post in this newsgroup, i hope my english is not too bad... Let's get straight to the point ! I have a little probleme using threads in my little training example :...
12
1721
by: Gurpreet Sachdeva | last post by:
I have written a code to figure out the difference in excecution time of a func before and after using threading... #!/usr/bin/env python import threading import time loops =
77
5208
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
7
2980
by: Microsoft | last post by:
I'm not sure where to physically place my subroutines in vb.net I get namespace and not declared errors... Imports System Imports System.Management Public Class Form1
3
1356
by: OpticTygre | last post by:
I have a class, ProcessFiles, with several subroutines it runs for each type of file I want to "process." First, I check directories for files. Then, based on the filenames of those I find in...
3
1366
by: JMax | last post by:
Hello! I have a C# ASPX page that displays a large amount of information. The software loops through 500 lines of code (and some of the 500 lines are looped multiple times, in a loop)....
7
2348
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
10
3689
by: Mike P | last post by:
I have two loops that I am running, one of which is calling a method which contains the second one. What I want to happen is for when the second loop exits (the inner loop), then the code will...
10
2896
by: liz0001 | last post by:
Hi, I need to access an .asp subroutine file from a subdomain. i.e. the website is at www.domain.com and I want to access the same subroutines on the subdomain mobile.domain.com. The...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.