Timer not Firing

davele

Member
Joined
Sep 29, 2009
Messages
12
Programming Experience
5-10
I'm trying to enable a timer from a SerialPort_DataReceived event. I can see that the timer is enabled, but the event does not fire.

It's rather simple, but I need some help understanding why it doesn't run the code in TimerWait_Tick.

Thanks,

Dave

Private WithEvents Dave As New System.IO.Ports.SerialPort
Private WithEvents timerWait As New Timer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
timerWait.Enabled = False
Dave.BaudRate = "9600"
Dave.StopBits = IO.Ports.StopBits.One
Dave.PortName = "COM1"
Dave.DataBits = 8
Dave.Open()
Button1.Enabled = False
End Sub

Private Sub Dave_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Dave.DataReceived
timerWait.Interval = 200
Me.timerWait.Enabled = True
End Sub

Private Sub TimerWait_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timerWait.Tick
If IsNumeric(Label1.Text) Then
Label1.Text = Val(Label1.Text) + 1
Else
Label1.Text = "0"
End If
End Sub
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
Just working with the SerialPort class myself for a scale. Instead of a timer, use a stopwatch object and compare the timespan.
 

davele

Member
Joined
Sep 29, 2009
Messages
12
Programming Experience
5-10
Timer Object

Hi Tom,

I guess I'm trying to better understand the .NET framework and the objects that are available. What's funny is that if I perform display the status of the timer, it says "TRUE", it just isn't firing the event.

I'm still new to the interactions between objects depends on where they are called and their state.

Thanks,

Dave
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
I think your timer is firing, although it is set so fast that its not giving the GUI a chance to refresh and show you the changing values.
 

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
You can always set a breakpoint to confirm it is firing, but there is no reason it shouldn't. 200 ms does seem fast, have tried slowing it down? Also check the DataReceived event and make sure it's firing as well - cause if it does not your timer is not turning on.
 

davele

Member
Joined
Sep 29, 2009
Messages
12
Programming Experience
5-10
Event Firing

The DataReceived Event is firing and confirmed by a breakpoint. I can also place a button on the form and display a message box Displaying that TimerWait.Enabled = True.

If I Place a button to enable the timer, it fires no problem. It almost seems like a thread issue?

20 Lines into .net and I'm not liking it already! Just kidding, I know it's because I don't understand, but I'm trying.

Thanks for your help.
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
Since you have confirmed that it is firing; again I say it is working. Your just not visibly seeing the results since its running so fast that its not giving the form a chance to refresh and show you the changes.

I wouldnt suggest this for distribution, it will greatly slow your apps running but for testing add an "Application.DoEvents" in your timers tick event. This will cause a pause after updating the label so that you can visibly see the changes.
 

davele

Member
Joined
Sep 29, 2009
Messages
12
Programming Experience
5-10
The event will not fire if it is enabled by the DataReceived Event.

The event will fire if it is enabled by a button click event.

I can confirm that the DataRecieved event does enable the timer because I can display a message box displaying the status of the timer.

The the event in the timer does not fire (it's a message box, so it should stop all of the action). I've also tried incrementing a public counter and display it's contents. I've put a breakline on the timer event code and it simply never makes it there.
 

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
That does not make any sense. Is there any more code in the DataReceived event sub that you haven't posted to us?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,517
Location
Norway
Programming Experience
10+
The DataReceived Event is firing and confirmed by a breakpoint. I can also place a button on the form and display a message box Displaying that TimerWait.Enabled = True.

If I Place a button to enable the timer, it fires no problem. It almost seems like a thread issue?
That is correct, DataReceived Event is asynchronous and raised from a secondary thread, which means you usually have to make an invoke call on UI thread to change a control:
VB.NET:
Me.Invoke(New MethodInvoker(AddressOf Me.Timer1.Start))
This is also explained in help for SerialPort.DataReceived Event (System.IO.Ports)

The reason for the erratic behaviour with no threading exception is probably because Forms.Timer is a Component and not a Control.

In this case though, you don't need to change the timer for each data received, you can start the timer from the same button event that starts the port.
 
Top Bottom