Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > Add timer to console application...
 

Add timer to console application...

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
BlackPepper  Wednesday, December 12, 2007 5:16 AM

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

SMD-  Wednesday, December 12, 2007 3:00 PM

use system.threading.thread.currenthread.sleep(millisecond) if youu need to stop your program for some seconds

Vilsad - MCTS  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.

spotty  Wednesday, December 12, 2007 6:02 PM

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

SMD-  Wednesday, December 12, 2007 3:00 PM

use system.threading.thread.currenthread.sleep(millisecond) if youu need to stop your program for some seconds

Vilsad - MCTS  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.

spotty  Wednesday, December 12, 2007 6:02 PM

You can use google to search for other answers

Custom Search

More Threads

• Disabling a toolstripmenu item
• Can you position a toolstrip button?
• DateTimePicker - Arabic Date
• how to change the value of a cell in ann array multidimension
• indirect reference is being made
• Embedding a Flash as background of a form!
• VB6 to VB.Net -> Excel automation
• How to find if registry key EXISTS?
• VB.NET Express 2005 - JET Engine "unspecified error"
• Power control of windows: How to reboot and wake from hibernate/standby?