|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
trying to understand classescreated 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? 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? > cj wrote:
> Maybe I can make this a bit clearer. I'm trying now to write a class If you're doing some kind of logging then there are mechanisms in the> 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. 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: I wouldn't recommend keeping the file open all the time (when would you> 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. 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 > Yes> 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 Yes. If they are Public then they would be visible outside their class,> m_lock is visible. Can 2 classes have objects named m_lock and they not > interfere with each other? which wouldn't be appropriate, so make them Private. -- Larry Lard Replies to group please 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. > 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? >
Show quote
Hide quote
"Joe Van Meer" <jvanm***@eastlink.ca> wrote in message Naw, it's a shared instance. Automatically created by the framework...I 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 > > > posted a reply with a link that *should* explain it further. :) Mythran 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 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? >> > >
Show quote
Hide quote
"cj" <cj@nospam.nospam> wrote in message Instance and Shared Variables: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? > http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfvbspec7_5.asp Mythran 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 > > 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 > > > > cj wrote:
[snippage] Show quoteHide quote > Stephany Young provided me with the following code to count threads "Instantiate". And it doesn't need you to instantiate anything, because> 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. 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 This line:> 0 on the very first time a sub in the class is referenced? > Private Shared m_threadcount As Int32 = 0 There can be some intricacies in how Shared member initializationworks, 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 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 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 > >
initialize a complex constant in a class
SendKeys Doesn't Always Work? MDI Child Forms Resetting Properties Panel GetChanges gives null column value in error RaiseEvent not working correctly Text from a Word.Doc Putting an application into my vb.net control (like a panel) How to query the key and value pair in ComboBox User interface ideas? |
|||||||||||||||||||||||