473,748 Members | 2,551 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 1533
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.Threadi ng.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 QueueUserWorkIt em 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 QueueUserWorkIt em:
http://msdn.microsoft.com/library/de...kitemtopic.asp
hope that helps..
Imran.

"OpticTygre " <op********@ade lphia.net> wrote in message
news:LO******** ************@ad elphia.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****@microso ft.com> wrote in message
news:eC******** ******@TK2MSFTN GP14.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.Threadi ng.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 QueueUserWorkIt em 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 QueueUserWorkIt em:
http://msdn.microsoft.com/library/de...kitemtopic.asp
hope that helps..
Imran.

"OpticTygre " <op********@ade lphia.net> wrote in message
news:LO******** ************@ad elphia.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********@ade lphia.net> wrote in message
news:hq******** ************@ad elphia.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.DoE vents() 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****@microso ft.com> wrote in message
news:Ow******** ********@tk2msf tngp13.phx.gbl. ..
"OpticTygre " <op********@ade lphia.net> wrote in message
news:hq******** ************@ad elphia.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********@ade lphia.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.DoE vents() 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
1647
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 : I wish to create two threads in my application, one thread (the subject) will increment a variable, and another thread (the controller) will print a message saying "i am waiting..." or someting like that, until the counter of the first thread...
12
1747
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
5372
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
7
3004
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
1373
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 the directory, I do IP address lookups from a database to see where these files need to be sent. After I lookup the IP addresses, I dimension a new class (ProcessThreads), and call ProcessThreads.Process for each IP I pull out of the database. In...
3
1384
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). Naturally, it takes approximately 7 seconds for the Page_Load event to fire (where I have all of my code).
7
2377
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 finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
10
3746
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 return to the first loop (the outer loop) and continue executing, but this doesn't seem to be happening. Here is my code if anybody can help me out : Loop 1 : for (int i = 0; i < oItems.Count; i++) {
10
2913
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 normal #include and #virtual don't seem to be working remotely. I have also tried:
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
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 we have to send another system
3
2217
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.