Hi!
Can someone explain me why I always end up with deadlock in the next piece of code. Deadlock occurs when I hit Button1 couple of times. Thanks!!!
[code=vbnet]
Public Module Module_Tags
'Tag
Public Class Tags
Public Shared objLock As New Object
Public Tag_Address As Integer
End Class
'Memory Integer
Public Class MI_Tag
Inherits Tags
Public Tag_Value As Short
Public To_Output As Short
Public Sub New()
MyBase.New()
End Sub
Public Shared Function Read_MemInteger s(ByVal Start_Address As Integer, ByVal Length As Integer, ByRef MemIntegers() As Short) As Boolean
Dim No_Error As Boolean
Try
SyncLock objLock
No_Error = mComDriver.Read _MemIntegers(St art_Address, Length, MemIntegers, 0)
End SyncLock
Return No_Error
Catch ex As Exception
Return False
End Try
End Function
Public Sub New(ByVal Address As Integer, ByVal Init As Short)
Tag_Address = Address
Tag_Value = Init
End Sub
Public Function To_Device() As Boolean
Dim No_Error As Boolean
Try
Dim MI() As Short = {To_Output}
SyncLock objLock
No_Error = mComDriver.Writ e_MemIntegers(T ag_Address, 1, MI, 0)
End SyncLock
Return No_Error
Catch ex As Exception
Return False
End Try
End Function
End Class
End Module
Public Class Main_Window
Public WithEvents Timer As New Timers.Timer
Public ExampleTag As New MI_Tag(20, 0)
Private Sub Main_Window_Loa d(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
Timer.Interval = 1000
Timer.Start()
End Sub
Public Sub Timer_Tick(ByVa l sender As Object, ByVal e As System.Timers.E lapsedEventArgs ) Handles Timer.Elapsed
Dim i() As Short
Dim a As Boolean
a = MI_Tag.Read_Mem Integers(0, 255, i)
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
ExampleTag.To_O utput = 5
ExampleTag.To_D evice()
End Sub
[/code]
Can someone explain me why I always end up with deadlock in the next piece of code. Deadlock occurs when I hit Button1 couple of times. Thanks!!!
[code=vbnet]
Public Module Module_Tags
'Tag
Public Class Tags
Public Shared objLock As New Object
Public Tag_Address As Integer
End Class
'Memory Integer
Public Class MI_Tag
Inherits Tags
Public Tag_Value As Short
Public To_Output As Short
Public Sub New()
MyBase.New()
End Sub
Public Shared Function Read_MemInteger s(ByVal Start_Address As Integer, ByVal Length As Integer, ByRef MemIntegers() As Short) As Boolean
Dim No_Error As Boolean
Try
SyncLock objLock
No_Error = mComDriver.Read _MemIntegers(St art_Address, Length, MemIntegers, 0)
End SyncLock
Return No_Error
Catch ex As Exception
Return False
End Try
End Function
Public Sub New(ByVal Address As Integer, ByVal Init As Short)
Tag_Address = Address
Tag_Value = Init
End Sub
Public Function To_Device() As Boolean
Dim No_Error As Boolean
Try
Dim MI() As Short = {To_Output}
SyncLock objLock
No_Error = mComDriver.Writ e_MemIntegers(T ag_Address, 1, MI, 0)
End SyncLock
Return No_Error
Catch ex As Exception
Return False
End Try
End Function
End Class
End Module
Public Class Main_Window
Public WithEvents Timer As New Timers.Timer
Public ExampleTag As New MI_Tag(20, 0)
Private Sub Main_Window_Loa d(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
Timer.Interval = 1000
Timer.Start()
End Sub
Public Sub Timer_Tick(ByVa l sender As Object, ByVal e As System.Timers.E lapsedEventArgs ) Handles Timer.Elapsed
Dim i() As Short
Dim a As Boolean
a = MI_Tag.Read_Mem Integers(0, 255, i)
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
ExampleTag.To_O utput = 5
ExampleTag.To_D evice()
End Sub
[/code]
Comment