System.Threading.Timer - Does it Work?

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
 
A

Alex Yakhnin [eMVP]

The System.Threading.Timer executes on a different thread.
You need to use Control.Invoke method to update the GUI
thread.

--
Alex Yakhnin, Microsoft Embedded MVP
IntelliProg, Inc.
http://www.intelliprog.com
"Check out our Compact Framework controls..."
-----Original Message-----
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, _
 
M

Mark McKnight

It's easier to use the System.Windows.Forms.Timer class. It does the
same thing, but it works in the UI thread with everything else.
 
G

Graeme Anderson

Thanks for that Mark, using forms.timer is a whole easier and does just what
is needed.

If anyone would like the code let me know.

thanks

Graeme
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top