Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Out of control threads

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ujjc001@gmail.com - 21 Feb 2007 17:08 GMT
Hello group.  I have a vb.net winform app that, in a sub, starts 10
new threads.  The sub waits in a loop for these threads to finish
before continuing.  The end of the sub thread's sub sets a boolean
which the main sub's loop checks for.  So, the problem is, that after
each thread completes it's task, memory is not released and the thread
count does not decrease.  The main sub loops for x number of items and
starts these 10 threads for each item.  I've used gc.collect and also
Thread.CurrentThread.Abort() at the end of each sub thread's sub.
I've disposed of all objects and set them = nothing and also tried
GC.GetTotalMemory(True) but nothing seems to want to let me release
these threads.
Any help would be much appreciated.
Thanks,
Jeff
Michael D. Ober - 21 Feb 2007 20:32 GMT
Instead of setting a boolean, create an array (or collection) of threads and
add each thread to it.  When you are ready to wait for the threads, do the
following (VB.NET):

for each th as Thread in myThreads
 th.Join
next th

This eliminates the boolean variable, which may be what is keeping the
thread in memory.

Mike Ober.

> Hello group.  I have a vb.net winform app that, in a sub, starts 10
> new threads.  The sub waits in a loop for these threads to finish
[quoted text clipped - 10 lines]
> Thanks,
> Jeff
Ashot Geodakov - 22 Feb 2007 23:49 GMT
Hi,

I am wondering why you need multithreading in this particular situation.
Your main sub waits for threads to finish anyway! Multithreading would make
sense only if you started some background task(s) and continued on doing
something else.

Why not to just execute these thread functions (without creating threads)
one aftert another, like this:

Sub s()
Begin
  ThreadFunction1()
  ThreadFunction2()
   ........
  ThreadFunctionN()
End

This way your sub doesn't have to monitor threads. It just exits after their
functions are all finished.

> Hello group.  I have a vb.net winform app that, in a sub, starts 10
> new threads.  The sub waits in a loop for these threads to finish
[quoted text clipped - 10 lines]
> Thanks,
> Jeff
mangist@gmail.com - 23 Feb 2007 03:03 GMT
> Hi,
>
[quoted text clipped - 37 lines]
>
> - Show quoted text -

Sometimes you may want to do 10 things in parralel (like visit 10
websites and scrape some info).  This is much faster when you use 10
threads, then doing it sequentially, as the bottleneck is latency, not
CPU.  I have personally used a method described above, and it works
great.

You may need to retrieve values from different databases, and then
perform some calculation using them.  You can only start the
calculation once you have the 10 values.
Ashot Geodakov - 23 Feb 2007 04:22 GMT
Hmm, and if you needed to visit 1000 websites (or databases) would you have
created a 1000 threads?

I am sure the method works great, but I still have doubts whether or not it
can give you any gain in performance.

There's still a network cable which allows only this much information to
pass at any moment. Say, 100 kbits per second. What if you need 100 kbits
from each of the 10 websites? They'll not come all in one second because of
multithreading. They'll take exactly 10 seconds.

And during multithreading doesn't CPU pass a token between threads and run
just a portion of a thread funtion and then move on to the next thread? How
is it different from running thread functions sequentially? Besides all the
extra steps that CPU has to take because of multithreading?

> Sometimes you may want to do 10 things in parralel (like visit 10
> websites and scrape some info).  This is much faster when you use 10
[quoted text clipped - 5 lines]
> perform some calculation using them.  You can only start the
> calculation once you have the 10 values.
Michael D. Ober - 23 Feb 2007 04:30 GMT
In this case, I would use the threadpool and queue up the threads.  You get
the benefits of the parallel operations but don't saturate your physical
resources.

Mike.

> Hmm, and if you needed to visit 1000 websites (or databases) would you
> have created a 1000 threads?
[quoted text clipped - 21 lines]
>> perform some calculation using them.  You can only start the
>> calculation once you have the 10 values.
ujjc001@gmail.com - 23 Feb 2007 21:15 GMT
FYI, I do need 10 threads, I have 10 things to do each which take
about 20 seconds, if I do sequentially, it would take 200 seconds, if
I use 10 threads it takes about 60 seconds, it's not 1 to 1 since
there are other factors like DB writing and such...

I'll try the pool.. Thanks.
ujjc001@gmail.com - 23 Feb 2007 21:19 GMT
Forgot to mention, I had to dispose of everything in any call made by
the thread before I could (and had to seemingly) abort the thread at
the end of the procedure.
I'd crash after 99+ threads and >100 mb of ram usage.  Once I properly
disposed of my objects (especially a memory stream. (oops)) it stayed
at 25 or so threads and 60 mb (which is reasonable for what I'm
doing).
Thanks again.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.