Home All Groups Group Topic Archive Search About

trying to understand classes

Author
22 Feb 2006 8:56 PM
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?

Author
22 Feb 2006 9:16 PM
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?


cj wrote:
Show quoteHide quote
> 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?
>
Author
23 Feb 2006 10:23 AM
Larry Lard
cj wrote:
> 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>
= <http://tinyurl.com/zprec> for more.

> 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.

--
Larry Lard
Replies to group please
Author
23 Feb 2006 1:47 PM
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.


Larry Lard wrote:
Show quoteHide quote
> cj wrote:
>> 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>
> = <http://tinyurl.com/zprec> for more.
>
>> 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.
>
Author
22 Feb 2006 9:17 PM
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



Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:uj$X$J$NGHA.740@TK2MSFTNGP12.phx.gbl...
> 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?
>
Author
22 Feb 2006 9:20 PM
Mythran
Show quote Hide quote
"Joe Van Meer" <jvanm***@eastlink.ca> wrote in message
news:%23QBkAV$NGHA.3988@TK2MSFTNGP09.phx.gbl...
> 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
Author
22 Feb 2006 9:28 PM
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?

Mythran wrote:
Show quoteHide quote
>
> "Joe Van Meer" <jvanm***@eastlink.ca> wrote in message
> news:%23QBkAV$NGHA.3988@TK2MSFTNGP09.phx.gbl...
>> 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
Author
22 Feb 2006 9:24 PM
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.

Joe Van Meer wrote:
Show quoteHide quote
> 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
>
>
>
> "cj" <cj@nospam.nospam> wrote in message
> news:uj$X$J$NGHA.740@TK2MSFTNGP12.phx.gbl...
>> 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?
>>
>
>
Author
22 Feb 2006 9:18 PM
Mythran
Show quote Hide quote
"cj" <cj@nospam.nospam> wrote in message
news:uj$X$J$NGHA.740@TK2MSFTNGP12.phx.gbl...
> 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
Author
22 Feb 2006 10:51 PM
TrtnJohn
Actually, I think this is the link that explains why there is no need to
create an instance of the class:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec7_1.asp

Show quoteHide quote
"Mythran" wrote:

>
> "cj" <cj@nospam.nospam> wrote in message
> news:uj$X$J$NGHA.740@TK2MSFTNGP12.phx.gbl...
> > 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
>
>
Author
23 Feb 2006 1:11 AM
TrtnJohn
Ooops, still wrong link :(  Here is the correct one:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec7_1_5.asp

Show quoteHide quote
"TrtnJohn" wrote:

> Actually, I think this is the link that explains why there is no need to
> create an instance of the class:
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec7_1.asp
>
> "Mythran" wrote:
>
> >
> > "cj" <cj@nospam.nospam> wrote in message
> > news:uj$X$J$NGHA.740@TK2MSFTNGP12.phx.gbl...
> > > 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
> >
> >
Author
23 Feb 2006 10:17 AM
Larry Lard
cj wrote:
[snippage]
Show quoteHide quote
> 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'.

--
Larry Lard
Replies to group please
Author
23 Feb 2006 10:47 AM
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
Author
23 Feb 2006 1:36 PM
cj
Thanks, that does help.

Cor Ligthert [MVP] wrote:
Show quoteHide quote
> 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
>
>