且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

试图理解课程

更新时间:2022-10-15 15:23:26

也许我可以让它更清楚一些。我现在正在努力写一个类

非常像这样,除了它有1个子行写一行到

txt文件。 class sub writeline将从许多

并发运行的线程中调用。


我心中的疑问:

1.哪里打开文件 - 我希望它在每次写入之前保持打开状态不打开

。我想我想在构造函数中做到这一点?

这个对吗? Stephany的程序似乎没有构造函数。然而

不知何故它只将m_threadcount初始化为0一次。


2.如果Stephany的类中的对象,m_lock用于锁定sub

只有一个线程一次使用它可以递增或递减

计数。出于好奇的缘故,我的新课程也可以有一个名为m_lock的b $ b,它与mythreadcount中的一个分开工作吗?

我知道我可以将其命名为其他东西。我想知道

m_lock在哪里可见。 2个类可以有名为m_lock的对象吗?它们不会互相干扰吗?

cj写道:
Stephany Young向我提供了以下代码计算由我的程序创建的线程。

Public Class MyThreadCount

私有共享m_lock作为新对象
私有共享m_threadcount为Int32 = 0

公共共享子增量()
SyncLock(m_lock)
m_threadcount + = 1
结束SyncLock
结束子

公共共享Sub Decrement()
SyncLock(m_lock)
m_threadcount - = 1
结束SyncLock
End Sub

公共共享ReadOnly属性ThreadCount()As Int32 Dim _count为Int32
SyncLock(m_lock)
_count = m_threadcount
结束SyncLock
返回_count
结束获取
结束属性
结束类

我通过调用
mythreadcount.increment
来使用它mythreadcount.decrement
label1.text = mythreadcount.threadcount

一切都很好,但我想知道它是如何开始的。我的意思是
我猜是怎么加载的。为什么我不需要煽动它或什么呢?怎么知道何时在第一次引用类中的子类时将变量m_threadcount分配给
0?



HI那里,


我想象代码中的某个地方实例化你的对象

喜欢:


Dim mythreadcount作为New MyThreadCount


或者可以分两步完成,首先是调暗它(在内存中腾出空间),然后是
然后实际实例化对象本身。


Dim mythreadcount为MyThreadCount

mythreadcount = New MyThreadCount


希望有所帮助,Joe


" cj" < cj@nospam.nospam>在消息中写道

新闻:uj ************* @ TK2MSFTNGP12.phx.gbl ...
Stephany Young向我提供了以下代码计算由我的程序创建的线程。

公共类MyThreadCount
私有共享m_lock作为新对象
私有共享m_threadcount为Int32 = 0

公共共享子增量()
SyncLock(m_lock)
m_threadcount + = 1
结束SyncLock
结束子

公共共享子递减()
SyncLock(m_lock)
m_threadcount - = 1
结束SyncLock
End Sub

公共共享ReadOnly属性ThreadCount()As Int32
获取
Dim _count为Int32
SyncLock(m_lock)
_count = m_threadcount
结束SyncLock 返回_count
结束获取
结束属性


我通过调用
mythreadcount.increment
mythreadcount.decrement来使用它
label1.text = mythreadcount.threadcount

一切都很好,但我想知道它是如何开始的。我的意思是
我猜是怎么加载的。为什么我不需要煽动它或什么呢?如何在第一次引用类中的子元素时知道何时将变量m_threadcount赋值为
0?





" cj" < cj@nospam.nospam>在消息中写道

新闻:uj ************* @ TK2MSFTNGP12.phx.gbl ...
Stephany Young向我提供了以下代码计算由我的程序创建的线程。

公共类MyThreadCount
私有共享m_lock作为新对象
私有共享m_threadcount为Int32 = 0

公共共享子增量()
SyncLock(m_lock)
m_threadcount + = 1
结束SyncLock
结束子

公共共享子递减()
SyncLock(m_lock)
m_threadcount - = 1
结束SyncLock
End Sub

公共共享ReadOnly属性ThreadCount()As Int32
获取
Dim _count为Int32
SyncLock(m_lock)
_count = m_threadcount
结束SyncLock
返回_count
结束获取
结束属性


我通过调用
mythreadcount.increment来使用
mythreadcount.decrement > label1.text = mythreadcount.threadcount

一切都很好,但我想知道它是如何开始的。我的意思是如何装载
我猜。为什么它不需要我煽动它或其他什么。
它是如何知道何时在第一次引用类中的子类时将变量m_threadcount分配给0?



实例和共享变量:
http://msdn.microsoft.com/library/en...fvbspec7_5.asp


Mythran


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?

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:
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?



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*************@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?




"cj" <cj@nospam.nospam> wrote in message
news:uj*************@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...fvbspec7_5.asp

Mythran