Add timer to console application...
-
Wednesday, December 12, 2007 5:16 AMI wanna build an application that to read a text file line by line...the textfile sample as below: "Message*TimeInSecond"apple*2tree*4house*3The 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.IOModule 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 = ""
TryDim 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 Whilesr.Close()
Catch ex As Exception
MsgBox(ex.Message)End Try
Console.ReadLine()End SubEnd Module
All Replies
-
Wednesday, December 12, 2007 3:00 PM
This is one way to implement a timer in a console app . . .
Code BlockModule 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 IntegerPrivate Declare Auto Function SetTimer Lib "user32" ( _
ByVal hWnd As IntPtr, _
ByVal nIDEvent As Integer, _
ByVal uElapse As Integer, _
ByVal lpTimerFunc As TimerProc) As IntegerSub 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 SubPublic 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 SubEnd Module
-
Wednesday, December 12, 2007 4:47 PM
use system.threading.thread.currenthread.sleep(millisecond) if youu need to stop your program for some seconds
-
Wednesday, December 12, 2007 6:02 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.

