Controlling a Timer with an External Input

Hello, im writing a vb program that is a graphical user interface for a robot. When the robot starts up it sends a command to PC telling the gui to start a timer showing how long the robot has been running. The only problem is that the timer never enables, It goes through the decision statement and says it should execute, but never does ( i put break points in after the hthe line of code where I enable the timer, and it says the timer should be enabled, I also put a set of break points in the timer ISR, but those are never tripped).

Basicaly what my problem boils down to is there anyway to set off a timer from an external input.

Thanks for your help

~Steve



Answer this question

Controlling a Timer with an External Input

  • jo123

    has the timers interval ever been set:

    Me.Timer1.Interval = 1000



  • Jean-Michel Herve

    For more info, check out http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=174589&SiteID=1

    Best regards,
    Johan Stenberg



  • Rohan Reddy

    See thats the weird thing, when i step through the code the program says that timer1.enabled = true (when the proper condition is met), but the timer routine never executes.
  • JimShort

    Try set it inside UI thread, I think it may not work correctly if it called from background thread.

  • Orest

    you can call from your form method named BeginInvoke(). In it's params you provide delegate to your another method, let say StartTimer. StartTimer will set Enabled to true. Also it's good to set Text property in UI thread too, it's not thread safe to set it in background thread.



  • Albert Yen -MS

    Hi!

    There is 3 Timer classes in .NET... which one you use

    Can you post code that you use to start timer

    In general you need to use System.Windows.Forms.Timer component, set Enabled property to true of false when external event happens and in timer handler you must increase elapsed seconds by 1 (timer resolution is 1000 ms).



  • Leena S

    Sorry, im not that familiar with the UI thread so you might have walk me through that, and the timer interval value was set in the properties window.
  • Marais_Kruger

    Here is my "Proof of Concept" code, just to see if i can get it to work.

    Imports System.IO.Ports

    Public Class Form1

    Dim seconds As Integer

    Private WithEvents objPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    CheckForIllegalCrossThreadCalls = False

    objPort = New SerialPort("COM4", 9600)

    objPort.ReceivedBytesThreshold = 2

    objPort.Open()

    End Sub

    Private Sub objPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles objPort.DataReceived

    Dim temp As String

    Dim strSerialPortData As String = objPort.ReadExisting

    temp = strSerialPortData

    TextBox2.Text = temp

    If strSerialPortData = "AA" Then

    Timer1.Enabled = True

    End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    seconds = seconds + 1

    TextBox1.Text = seconds

    TextBox3.Text = 1

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    objPort.Close()

    Me.Close()

    End Sub

    End Class

    Basically all i want it to do is to start a timer when i get a certain input from a micro controller. But for some strange reason when i debug the program the timer says it should be enabled but it never executes the timer1_Tick routine.


  • sqldbapree

    If I do remember all correct - SerialPort class raise events in background threads, so you can't use "TextBox2.Text = temp", it may throw an exception (and background threads do not raise any window to notify you about failure, so you won't see it). Are you sure "Timer1.Enabled=True" was executed

  • Controlling a Timer with an External Input