B
BoloBaby
All,
I believe I am having a threading problem. Class "BELights" is part of a
larger DLL that is used by my main application. A user control (of type
BESeat) within the main application raises an event and attempts to execute
the "StartTiming" method of BELights. The method does not execute which
leads me to believe that BELights is not thread-safe. (Or is it the user
control that is not thread safe?)
When an event fires for "mBECardReaderManager" - an object created by a
third DLL and used by the main application - the handler correctly executes
the "StartTiming" method of BELights. There doesn't seem to be any
threading issue in that part of the program, only between the user control
and BELights.
I've Googled this problem many times over and can't seem to find an answer
that makes any sense to me, therefore I'll ask here. How do I make BELights
thread-safe so that the desired method executes properly.
"Simplified" sample code follows with the relevant parts included.
'*****
'Pared down version of collection class - assume it is much more complicated
but that these are the relevant parts
'BELights is located in BEDIO.dll
'*****
Public Class BELights
Inherits CollectionBase
Public Event TimesUp(ByVal LightNumber As Integer)
Public Sub StartTiming(ByVal LightNumber As Integer, ByVal Duration As
Integer)
Dim pBELight As BELight
pBELight = list.Item(LightNumber - 1)
pBELight.StartTiming(Duration)
End Sub
Public Sub New()
Dim intCount As Integer
Dim pBELight As BELight
'add the lights
For intCount = 1 To 14
pBELight = New BELight
'add handlers for the two BELight events
AddHandler pBELight.LightChanged, AddressOf LightChanged
AddHandler pBELight.TimesUp, AddressOf LightTimesUp
pBELight.LightNumber = intCount
pBELight.Reset()
list.Add(pBELight)
Next
End Sub
Private Sub LightTimesUp(ByVal LightNumber As Integer)
RaiseEvent TimesUp(LightNumber)
End Sub
Private Sub LightChanged(ByVal LightNumber As Integer, ByVal LightStatus
As BELight.BELightStatus)
'do some irrelevant code...
End Sub
End Class
'*****
'BELights is accessed through this class which is included in BEDIO.dll
'Again, this class is simplified
'*****
Public Class BEDIO
Private WithEvents mBELights As New BELights
Public Event TimesUp(ByVal LightNumber As Integer)
Public ReadOnly Property Lights() As BELights
Get
Return mBELights
End Get
End Property
Private Sub mBELights_TimesUp(ByVal LightNumber As Integer) Handles
mBELights.TimesUp
RaiseEvent TimesUp(LightNumber)
End Sub
End Class
'*****
'My main application contains this code
'frmMain is the startup object
'*****
Public Class frmMain
Inherits System.Windows.Forms.Form
'****This form contains an instance of the user control type BESeat,
known as Seat1
'****Seat1 fires an event called "ManuallyStartTime" (see below for
handler)
'****ManuallyStartTime is fired as the result of the user selecting an
option from a ContextMenu
'****that is associated with Seat1
'****BECardReaderManager comes from BELib.dll
Public WithEvents mBECardReaderManager As New BECardReaderManager
Public WithEvents mBEDIO As New BEDIO.BEDIO
'****No threading problem in the event handler for mBECardReaderManager
'****mBEDIO.Lights.StartTiming works fine (the timing light comes on)
Private Sub mBECardReaderManager_BECardInserted(ByVal eCardReaderName As
String, ByVal eCardReaderNumber As Integer, ByVal eCardID As String) Handles
mBECardReaderManager.BECardInserted
mBEDIO.Lights.StartTiming(eCardReaderNumber, 240)
End Sub
'>>>>>mBEDIO.Lights.StartTiming does not work here<<<<<
'****(This sub actually handles many more SeatX.ManuallyStartTime events
than just the one shown)
Private Sub ManuallyStartTime(ByVal Seat As BESeat) Handles
Seat1.ManuallyStartTime
'Seat is a passed instance of Seat1, the object that fires the event
mBEDIO.Lights.StartTiming(Seat.LightNumber, 240)
End Sub
End Class
WHEW...
OK, if you made it that far, you are a real trooper. Also, I'd hope that my
problem is fairly clear. I don't have experience in making stuff like this
thread-safe... maybe you do. Any help would be greatly appreciated.
Thanks,
Gardner
I believe I am having a threading problem. Class "BELights" is part of a
larger DLL that is used by my main application. A user control (of type
BESeat) within the main application raises an event and attempts to execute
the "StartTiming" method of BELights. The method does not execute which
leads me to believe that BELights is not thread-safe. (Or is it the user
control that is not thread safe?)
When an event fires for "mBECardReaderManager" - an object created by a
third DLL and used by the main application - the handler correctly executes
the "StartTiming" method of BELights. There doesn't seem to be any
threading issue in that part of the program, only between the user control
and BELights.
I've Googled this problem many times over and can't seem to find an answer
that makes any sense to me, therefore I'll ask here. How do I make BELights
thread-safe so that the desired method executes properly.
"Simplified" sample code follows with the relevant parts included.
'*****
'Pared down version of collection class - assume it is much more complicated
but that these are the relevant parts
'BELights is located in BEDIO.dll
'*****
Public Class BELights
Inherits CollectionBase
Public Event TimesUp(ByVal LightNumber As Integer)
Public Sub StartTiming(ByVal LightNumber As Integer, ByVal Duration As
Integer)
Dim pBELight As BELight
pBELight = list.Item(LightNumber - 1)
pBELight.StartTiming(Duration)
End Sub
Public Sub New()
Dim intCount As Integer
Dim pBELight As BELight
'add the lights
For intCount = 1 To 14
pBELight = New BELight
'add handlers for the two BELight events
AddHandler pBELight.LightChanged, AddressOf LightChanged
AddHandler pBELight.TimesUp, AddressOf LightTimesUp
pBELight.LightNumber = intCount
pBELight.Reset()
list.Add(pBELight)
Next
End Sub
Private Sub LightTimesUp(ByVal LightNumber As Integer)
RaiseEvent TimesUp(LightNumber)
End Sub
Private Sub LightChanged(ByVal LightNumber As Integer, ByVal LightStatus
As BELight.BELightStatus)
'do some irrelevant code...
End Sub
End Class
'*****
'BELights is accessed through this class which is included in BEDIO.dll
'Again, this class is simplified
'*****
Public Class BEDIO
Private WithEvents mBELights As New BELights
Public Event TimesUp(ByVal LightNumber As Integer)
Public ReadOnly Property Lights() As BELights
Get
Return mBELights
End Get
End Property
Private Sub mBELights_TimesUp(ByVal LightNumber As Integer) Handles
mBELights.TimesUp
RaiseEvent TimesUp(LightNumber)
End Sub
End Class
'*****
'My main application contains this code
'frmMain is the startup object
'*****
Public Class frmMain
Inherits System.Windows.Forms.Form
'****This form contains an instance of the user control type BESeat,
known as Seat1
'****Seat1 fires an event called "ManuallyStartTime" (see below for
handler)
'****ManuallyStartTime is fired as the result of the user selecting an
option from a ContextMenu
'****that is associated with Seat1
'****BECardReaderManager comes from BELib.dll
Public WithEvents mBECardReaderManager As New BECardReaderManager
Public WithEvents mBEDIO As New BEDIO.BEDIO
'****No threading problem in the event handler for mBECardReaderManager
'****mBEDIO.Lights.StartTiming works fine (the timing light comes on)
Private Sub mBECardReaderManager_BECardInserted(ByVal eCardReaderName As
String, ByVal eCardReaderNumber As Integer, ByVal eCardID As String) Handles
mBECardReaderManager.BECardInserted
mBEDIO.Lights.StartTiming(eCardReaderNumber, 240)
End Sub
'>>>>>mBEDIO.Lights.StartTiming does not work here<<<<<
'****(This sub actually handles many more SeatX.ManuallyStartTime events
than just the one shown)
Private Sub ManuallyStartTime(ByVal Seat As BESeat) Handles
Seat1.ManuallyStartTime
'Seat is a passed instance of Seat1, the object that fires the event
mBEDIO.Lights.StartTiming(Seat.LightNumber, 240)
End Sub
End Class
WHEW...
OK, if you made it that far, you are a real trooper. Also, I'd hope that my
problem is fairly clear. I don't have experience in making stuff like this
thread-safe... maybe you do. Any help would be greatly appreciated.
Thanks,
Gardner