G
Graeme Anderson
I have a need to code a timer in a class which will raise events
periodically. The timer used is system.threading.timer and it works well
EXCEPT when I call RaiseEvent to notify a windows form. There appears to be
a deadlock occurring as I don't get control back to the class.
This is required on a Pocket PC Application when connecting to GPRS.
The same code works fine as a Windows application - have taken the liberty
of including a code snippet below.
Any suggestions welcomed,
thanks
Graeme A
--->> put this into a class module
Namespace Lfx.Handheld.classes
Public Class MyApplication
Private MyTimer As System.Threading.Timer
Public Shared Event Event_LoginState(ByVal EventMessage As String,
ByVal ProcessCounter As Integer)
Private Sub DoWork()
' raise event back to calling process in case that process needs
to use the events
RaiseEvent Event_LoginState(Now.ToLongTimeString, Now.Second)
SetupTimers()
End Sub
Private Sub OnTimer(ByVal o As Object)
MyTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite)
DoWork()
End Sub
Private Sub SetupTimers()
Dim callback As System.Threading.TimerCallback
callback = New System.Threading.TimerCallback(AddressOf OnTimer)
MyTimer = New System.Threading.Timer(callback, "", _
System.Threading.Timeout.Infinite, _
System.Threading.Timeout.Infinite)
MyTimer.Change(1000, System.Threading.Timeout.Infinite)
End Sub
Public Sub New()
SetupTimers()
End Sub
End Class
End Namespace
--->> put this into a form with appropriate buttons and status bar
Dim lfx_timer As lfx.Handheld.classes.MyApplication
Public Sub LoginState(ByVal Message As String, ByVal ProcessCounter
As Integer)
' process events back from main controller
Try
---> currently gets here then locks!
lblProcessCaption.Text = Message.ToString
prog.Value = ProcessCounter
Catch ex As Exception
MessageBox.Show("System Error " & ex.Message.ToString)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
lfx_timer = New lfx.Handheld.classes.MyApplication()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler lfx.Handheld.classes.MyApplication.Event_LoginState,
AddressOf LoginState
End Sub
periodically. The timer used is system.threading.timer and it works well
EXCEPT when I call RaiseEvent to notify a windows form. There appears to be
a deadlock occurring as I don't get control back to the class.
This is required on a Pocket PC Application when connecting to GPRS.
The same code works fine as a Windows application - have taken the liberty
of including a code snippet below.
Any suggestions welcomed,
thanks
Graeme A
--->> put this into a class module
Namespace Lfx.Handheld.classes
Public Class MyApplication
Private MyTimer As System.Threading.Timer
Public Shared Event Event_LoginState(ByVal EventMessage As String,
ByVal ProcessCounter As Integer)
Private Sub DoWork()
' raise event back to calling process in case that process needs
to use the events
RaiseEvent Event_LoginState(Now.ToLongTimeString, Now.Second)
SetupTimers()
End Sub
Private Sub OnTimer(ByVal o As Object)
MyTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite)
DoWork()
End Sub
Private Sub SetupTimers()
Dim callback As System.Threading.TimerCallback
callback = New System.Threading.TimerCallback(AddressOf OnTimer)
MyTimer = New System.Threading.Timer(callback, "", _
System.Threading.Timeout.Infinite, _
System.Threading.Timeout.Infinite)
MyTimer.Change(1000, System.Threading.Timeout.Infinite)
End Sub
Public Sub New()
SetupTimers()
End Sub
End Class
End Namespace
--->> put this into a form with appropriate buttons and status bar
Dim lfx_timer As lfx.Handheld.classes.MyApplication
Public Sub LoginState(ByVal Message As String, ByVal ProcessCounter
As Integer)
' process events back from main controller
Try
---> currently gets here then locks!
lblProcessCaption.Text = Message.ToString
prog.Value = ProcessCounter
Catch ex As Exception
MessageBox.Show("System Error " & ex.Message.ToString)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
lfx_timer = New lfx.Handheld.classes.MyApplication()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler lfx.Handheld.classes.MyApplication.Event_LoginState,
AddressOf LoginState
End Sub