473,659 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multithreading question

I am using VB.NET 2005 and thread.
In each thread I open a database connection and before it exits the thread I
close the database connection.
Is there any way for me to count how many threads are still working so that
if I know there is say 50 threads are working, I want to hold on and do not
create any more thread until the previous threads are finished ?
Thank you
Jun 27 '08 #1
6 1032
I'm not one of the heavy hitters here, but what I do in a similar situation
is to use a simple counter. In my case the incrementing doesn't need to be
synchronized. To decrement the counter I use ...

SitesToGo = Interlocked.Dec rement(globalin f.TotalSites)

If you need to synchronize the incrementing I am sure there is an
Interlock.Incre ment method.

When you use counters like this you have to be very sure they get
incremented and decremented in all the right places. It's easy to miss
error handling paths.

Bob

"fniles" <fn****@pfmail. comwrote in message
news:eg******** ******@TK2MSFTN GP04.phx.gbl...
>I am using VB.NET 2005 and thread.
In each thread I open a database connection and before it exits the thread
I close the database connection.
Is there any way for me to count how many threads are still working so
that if I know there is say 50 threads are working, I want to hold on and
do not create any more thread until the previous threads are finished ?
Thank you

Jun 27 '08 #2
If you use a threadpool you can,

http://msdn.microsoft.com/en-us/libr...ol(VS.71).aspx

However why are you using a thread to open a connection, as that is the
first step in some real synchronous actions.

(Not meant for you, you are probably paid for the time your programs consume
processor time, however for others who get this idea)

Cor

"fniles" <fn****@pfmail. comschreef in bericht
news:eg******** ******@TK2MSFTN GP04.phx.gbl...
>I am using VB.NET 2005 and thread.
In each thread I open a database connection and before it exits the thread
I close the database connection.
Is there any way for me to count how many threads are still working so
that if I know there is say 50 threads are working, I want to hold on and
do not create any more thread until the previous threads are finished ?
Thank you
Jun 27 '08 #3
Each thread knows it's number. You could log them in some way. Maybe
have each one set it's element in an array to true coming in and false
going out and you could run the array and count bits before starting up
another one. Fairly cheap on processor time, I expect.

I have been wondering about how to serialize also. No answer seen yet.
Still hoping...

Mike

On Wed, 21 May 2008 15:42:42 -0500, in
microsoft.publi c.dotnet.langua ges.vb "fniles" <fn****@pfmail. comwrote:
>I am using VB.NET 2005 and thread.
In each thread I open a database connection and before it exits the thread I
close the database connection.
Is there any way for me to count how many threads are still working so that
if I know there is say 50 threads are working, I want to hold on and do not
create any more thread until the previous threads are finished ?
Thank you
Jun 27 '08 #4
cj
I have a class:

Public Class MyThreadCount

Private Shared m_lock As New Object
Private Shared m_threadcount As Int32 = 0

Public Shared Sub Increment()
SyncLock (m_lock)
m_threadcount += 1
End SyncLock
End Sub

Public Shared Sub Decrement()
SyncLock (m_lock)
m_threadcount -= 1
End SyncLock
End Sub

Public Shared ReadOnly Property ThreadCount() As Int32
Get
Dim _count As Int32
SyncLock (m_lock)
_count = m_threadcount
End SyncLock
Return _count
End Get
End Property
End Class

When a thread starts it increments the counter:

MyThreadCount.I ncrement()

When it ends it decrements the counter:

MyThreadCount.D ecrement()

The main program has a timer that looks at MyThreadCount.T hreadCount and
displays how many threads are currently running as an indicator of how
much work the program is doing. Also when the program goes to shutdown
I stop creating new threads and wait for the threadcount to come down to
0 before stopping.

This seems like what Bob suggested but I'm not familiar with Interlock.
Perhaps it does what synclock does and prevents multiple threads from
stepping on each other when incrementing and decrementing the counter.
fniles wrote:
I am using VB.NET 2005 and thread.
In each thread I open a database connection and before it exits the thread I
close the database connection.
Is there any way for me to count how many threads are still working so that
if I know there is say 50 threads are working, I want to hold on and do not
create any more thread until the previous threads are finished ?
Thank you

Jun 27 '08 #5
Thank you.
>However why are you using a thread to open a connection, as that is the
first step in some real synchronous actions.
In the thread I need to read a few queries from the database, if I open the
connection in the main program and sends the connection to the thread, won't
I run into the error "you can only have 1 datareader per connection", or
something like that ?

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:D8******** *************** ***********@mic rosoft.com...
If you use a threadpool you can,

http://msdn.microsoft.com/en-us/libr...ol(VS.71).aspx

However why are you using a thread to open a connection, as that is the
first step in some real synchronous actions.

(Not meant for you, you are probably paid for the time your programs
consume processor time, however for others who get this idea)

Cor

"fniles" <fn****@pfmail. comschreef in bericht
news:eg******** ******@TK2MSFTN GP04.phx.gbl...
>>I am using VB.NET 2005 and thread.
In each thread I open a database connection and before it exits the
thread I close the database connection.
Is there any way for me to count how many threads are still working so
that if I know there is say 50 threads are working, I want to hold on and
do not create any more thread until the previous threads are finished ?
Thank you

Jun 27 '08 #6
Thank you.

Can I do the following:
1. check the number of threads
2. if the number of threads >= 100 then loop until the number of threads <
100
like so:

Dim EachMessageThre ad As Threading.Threa d = Nothing

EachMessageThre ad = New Threading.Threa d(AddressOf
clsEachMessage. ProcessMessage)
do while myThreadCount.T hreadCount >= 100
thread.sleep(10 0)
loop
MyThreadCount.I ncrement()
EachMessageThre ad.Start()

"cj" <cj@nospam.nosp amwrote in message
news:Os******** ******@TK2MSFTN GP05.phx.gbl...
>I have a class:

Public Class MyThreadCount

Private Shared m_lock As New Object
Private Shared m_threadcount As Int32 = 0

Public Shared Sub Increment()
SyncLock (m_lock)
m_threadcount += 1
End SyncLock
End Sub

Public Shared Sub Decrement()
SyncLock (m_lock)
m_threadcount -= 1
End SyncLock
End Sub

Public Shared ReadOnly Property ThreadCount() As Int32
Get
Dim _count As Int32
SyncLock (m_lock)
_count = m_threadcount
End SyncLock
Return _count
End Get
End Property
End Class

When a thread starts it increments the counter:

MyThreadCount.I ncrement()

When it ends it decrements the counter:

MyThreadCount.D ecrement()

The main program has a timer that looks at MyThreadCount.T hreadCount and
displays how many threads are currently running as an indicator of how
much work the program is doing. Also when the program goes to shutdown I
stop creating new threads and wait for the threadcount to come down to 0
before stopping.

This seems like what Bob suggested but I'm not familiar with Interlock.
Perhaps it does what synclock does and prevents multiple threads from
stepping on each other when incrementing and decrementing the counter.
fniles wrote:
>I am using VB.NET 2005 and thread.
In each thread I open a database connection and before it exits the
thread I close the database connection.
Is there any way for me to count how many threads are still working so
that if I know there is say 50 threads are working, I want to hold on and
do not create any more thread until the previous threads are finished ?
Thank you

Jun 27 '08 #7

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

Similar topics

1
2048
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able to kick off the process and continue to work in the appliaction while getting progress updates and the ability to cancel. The method that seems easiest to me is this: The class exposes certain events for progress. Start, ProgressUpdate, and...
11
4253
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
16
8493
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
2
2307
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and I just wanted to ask if I am missing anything based on the following scenario. My test app pulls data from a large external data source which has a table-like structure (but not rdbms - more
1
326
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able to kick off the process and continue to work in the appliaction while getting progress updates and the ability to cancel. The method that seems easiest to me is this: The class exposes certain events for progress. Start, ProgressUpdate, and...
0
8748
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
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
7359
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...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
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
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
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.