trying to understand classes

C

cj

Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean
how is it loaded I guess. why doesn't it need me to instigate it or
whatever. how does it know when to assign the variable m_threadcount to
0 on the very first time a sub in the class is referenced?
 
C

cj

Maybe I can make this a bit clearer. I'm trying now to write a class
very much like this except it will have 1 sub which writes a line to a
txt file. The classes sub writeline will be called from withing many
concurrently running threads.

Questions in my mind:
1. Where do I open the file--I want it to stay open not be opened just
prior to each write. I think I'd want to do that in a constructor? Is
this right? Stephany's program doesn't seem to have a constructor. Yet
somehow it initializes m_threadcount to 0 only once.

2. If the object in Stephany's class, m_lock is used to lock the sub so
only one thread uses it at a time can be incrementing or decrementing
the count. Just for curiosity sake can my new class also have a object
called m_lock that would work separately from the one in mythreadcount?
I know I can name it something else. I'm trying to understand where
m_lock is visible. Can 2 classes have objects named m_lock and they not
interfere with each other?
 
J

Joe Van Meer

HI there,

I imagine somewhere in that code is a line the instantiates your object
like:

Dim mythreadcount as New MyThreadCount

Or could be done in 2 steps, first is dimming it (making space in memory),
then the actual instantiation of the object itself.

Dim mythreadcount as MyThreadCount
mythreadcount = New MyThreadCount

Hope that helps, Joe
 
M

Mythran

cj said:
Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean how
is it loaded I guess. why doesn't it need me to instigate it or whatever.
how does it know when to assign the variable m_threadcount to 0 on the
very first time a sub in the class is referenced?

Instance and Shared Variables:
http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfvbspec7_5.asp

Mythran
 
M

Mythran

Joe Van Meer said:
HI there,

I imagine somewhere in that code is a line the instantiates your object
like:

Dim mythreadcount as New MyThreadCount

Or could be done in 2 steps, first is dimming it (making space in memory),
then the actual instantiation of the object itself.

Dim mythreadcount as MyThreadCount
mythreadcount = New MyThreadCount

Hope that helps, Joe

Naw, it's a shared instance. Automatically created by the framework...I
posted a reply with a link that *should* explain it further. :)

Mythran
 
C

cj

That's what puzzles me. I didn't do that anywhere. It still works.
I'm thinking it's not an instance of the class I use but the class
itself and that bothers me too cause I just can't seem to understand. I
get stuff to work but really don't know why.
 
C

cj

I'm afraid most links from MS don't make much sense to me. Sorry. I
take it .net makes the instance for me but when does it determine an
instance needs to be made?
 
L

Larry Lard

cj wrote:
[snippage]
Stephany Young provided me with the following code to count threads
created by my program.

Public Class MyThreadCount

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

Public Shared Sub Increment()
Public Shared Sub Decrement()
Public Shared ReadOnly Property ThreadCount() As Int32
Everything works great but I'm wondering how it gets started. I mean
how is it loaded I guess. why doesn't it need me to instigate it or
whatever.

"Instantiate". And it doesn't need you to instantiate anything, because
all the variables and methods are Shared. I know you said you don't
really get links to the .NET documentation, but you really should take
the time to learn the difference between instance members (ones without
the Shared tag) and Shared members. It's pretty fundamental, and better
suited to being explained by a nice book or web page than by us in a
newsgroup post.
how does it know when to assign the variable m_threadcount to
0 on the very first time a sub in the class is referenced?

This line:
Private Shared m_threadcount As Int32 = 0

There can be some intricacies in how Shared member initialization
works, but for the most part it's safe to assume that it happens 'at
the start of the program'.
 
L

Larry Lard

cj said:
Maybe I can make this a bit clearer. I'm trying now to write a class
very much like this except it will have 1 sub which writes a line to a
txt file. The classes sub writeline will be called from withing many
concurrently running threads.

If you're doing some kind of logging then there are mechanisms in the
Framework that will handle this better than any user code. See
<http://msdn.microsoft.com/library/en-us/vbcon/html/vbconIntroductionToInstrumentationTracing.asp>
= said:
Questions in my mind:
1. Where do I open the file--I want it to stay open not be opened just
prior to each write. I think I'd want to do that in a constructor? Is
this right? Stephany's program doesn't seem to have a constructor. Yet
somehow it initializes m_threadcount to 0 only once.

I wouldn't recommend keeping the file open all the time (when would you
close it?). The reason Stephany's code doesn't need an explicit
constructor is because the only initialization it does can be specified
as a variable initialization. If you had, say, a variable you wanted
initialized to the day of the month (ie a non-constant) you would do
something like

Private Shared dayOfMonth As Integer

Public Shared Sub New()
' this is the shared constructor
dayOfMonth = Today.Day
End Sub
2. If the object in Stephany's class, m_lock is used to lock the sub so
only one thread uses it at a time can be incrementing or decrementing
the count. Just for curiosity sake can my new class also have a object
called m_lock that would work separately from the one in mythreadcount?
Yes

I know I can name it something else. I'm trying to understand where
m_lock is visible. Can 2 classes have objects named m_lock and they not
interfere with each other?

Yes. If they are Public then they would be visible outside their class,
which wouldn't be appropriate, so make them Private.
 
C

Cor Ligthert [MVP]

cj,

A class with only shared members is in my opinion in fact not a class it is
a module.

It is direct instanced (and that is the only reason people can call it a
class) in your main program thread and will never leave it, in the same way
as with a module. The advance of an Class above a module in VBNet is that
you can describe it as a class and call the members as in a class by
instance Cj.Mymethode, with what it is more descripting.

For me a real class creates (instances) an object on the managed heap. If
that object is not needed anymore than it is released (the memory is given
back) automaticly by the garbage collector. What is one of the advantages of
managed code.

I hope that this gives an idea.

Cor
 
C

cj

Yes, I'm logging. the logging in the framework seems like another class
of worms that I don't have time to deal with if I opened them.

For performance sake I think the file does need to stay open--I can't
afford the time for it to open, write and close each time. It'll close
when the program closes. I've got multiple threads running so all of
them could conceivably be trying to update the file at the same time. I
don't know what the framework logging does about this but I know using
the basic structure of Stephany's code I can write the line myself while
ensuring threads don't step on each other. I'm just not sure if the
file is opened by the program when it starts if the threads that break
off from the program will be able to see it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top