none
Add timer to console application...

    Question

  • I wanna build an application that to read a text file line by line...
    the textfile sample as below: "Message*TimeInSecond"
    apple*2
    tree*4
    house*3
     
    The question is how can I run a timer to display a text according to the time in my textfile?...
    like display apple 2 seconds, tree 4 seconds, house 3 seconds and loop back to apple 2 seconds.................
     
    i m noob in vb.net dunno how to use timer control in console...
     
     
    My code as below...thanx for help...
     
    Imports System.IO
     
    Module Module1
        Public seperator As String = "*"
        Public Path As String = "C:\Test.txt"
     
     
     
        Sub Main()
     
         
                Dim myAr() As String
                Dim pathname As String = ""
                Dim dtime As String = ""
                Dim LineIn As String = ""
                Try
     
                    Dim sr As StreamReader = File.OpenText(Path)
     
                    While sr.Peek <> -1
                        LineIn = sr.ReadLine()
                        myAr = LineIn.Split("*")
     
                        pathname = myAr(0)
                        dtime = myAr(1)
                        Console.WriteLine(pathname)
                        Console.WriteLine(dtime)
     
     
     
                    End While
     
                    sr.Close()
     

                Catch ex As Exception
     

                    MsgBox(ex.Message)
     
                End Try
     

                Console.ReadLine()
     
     
     
        End Sub
     
     
     
    End Module
    Wednesday, December 12, 2007 5:16 AM

Answers

  • This is one way to implement a timer in a console app . . .

     

    Code Block

    Module Module1

     

    Private m_TimerID As Integer

     

    Private Delegate Sub TimerProc(ByVal hWnd As IntPtr, _
        ByVal uMsg As Integer, _
        ByVal idEvent As Integer, _
        ByVal dwTime As Integer)

     

    Private Declare Auto Function KillTimer Lib "user32" ( _
        ByVal hWnd As IntPtr, _
        ByVal nIDEvent As Integer) As Integer

     

    Private Declare Auto Function SetTimer Lib "user32" ( _
        ByVal hWnd As IntPtr, _
        ByVal nIDEvent As Integer, _
        ByVal uElapse As Integer, _
        ByVal lpTimerFunc As TimerProc) As Integer

     

    Sub Main()
        m_TimerID = SetTimer(IntPtr.Zero, 0, 3000, AddressOf TimerEvent)
        Dim s As String = InputBox("Keep the app running while we wait for the timer event")
    End Sub

     

    Public Sub TimerEvent(ByVal hWnd As IntPtr, _
        ByVal uMsg As Integer, _
        ByVal idEvent As Integer, _
        ByVal dwTime As Integer)

     

        Dim rc As Integer = KillTimer(IntPtr.Zero, m_TimerID)
        MsgBox("The timer event has fired!")
    End Sub

     

    End Module

     

     

     

    Wednesday, December 12, 2007 3:00 PM
  • use  system.threading.thread.currenthread.sleep(millisecond) if youu need to stop your program for some seconds

    Wednesday, December 12, 2007 4:47 PM
  • Using thread.sleep to cause delays is often qutoed as a simple way to achieve the result.

     

    Do not use threading.sleep to cause a delay in a program.

     

    This is really bad practice to use this outside of some very small scenarios.    It stops the entire execution of the thread and for when used causes application to become unresponsive.

     

     

     

    Wednesday, December 12, 2007 6:02 PM

All replies

  • This is one way to implement a timer in a console app . . .

     

    Code Block

    Module Module1

     

    Private m_TimerID As Integer

     

    Private Delegate Sub TimerProc(ByVal hWnd As IntPtr, _
        ByVal uMsg As Integer, _
        ByVal idEvent As Integer, _
        ByVal dwTime As Integer)

     

    Private Declare Auto Function KillTimer Lib "user32" ( _
        ByVal hWnd As IntPtr, _
        ByVal nIDEvent As Integer) As Integer

     

    Private Declare Auto Function SetTimer Lib "user32" ( _
        ByVal hWnd As IntPtr, _
        ByVal nIDEvent As Integer, _
        ByVal uElapse As Integer, _
        ByVal lpTimerFunc As TimerProc) As Integer

     

    Sub Main()
        m_TimerID = SetTimer(IntPtr.Zero, 0, 3000, AddressOf TimerEvent)
        Dim s As String = InputBox("Keep the app running while we wait for the timer event")
    End Sub

     

    Public Sub TimerEvent(ByVal hWnd As IntPtr, _
        ByVal uMsg As Integer, _
        ByVal idEvent As Integer, _
        ByVal dwTime As Integer)

     

        Dim rc As Integer = KillTimer(IntPtr.Zero, m_TimerID)
        MsgBox("The timer event has fired!")
    End Sub

     

    End Module

     

     

     

    Wednesday, December 12, 2007 3:00 PM
  • use  system.threading.thread.currenthread.sleep(millisecond) if youu need to stop your program for some seconds

    Wednesday, December 12, 2007 4:47 PM
  • Using thread.sleep to cause delays is often qutoed as a simple way to achieve the result.

     

    Do not use threading.sleep to cause a delay in a program.

     

    This is really bad practice to use this outside of some very small scenarios.    It stops the entire execution of the thread and for when used causes application to become unresponsive.

     

     

     

    Wednesday, December 12, 2007 6:02 PM