Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Showing Current Date and Time without using Timer in VB.Net 2005?
 

Showing Current Date and Time without using Timer in VB.Net 2005?

On a label I am showing current Date and Time using one timer.
What I did is set the interval of that timer for 1 Seconds.Then on the tick

event of timer I written code as - lblDate.Text = Now()
Now problem is that this timer control is heavy weight process, instead of this

if I could do it using thread then it will be a light weighted process.
So I want to know whether I could do it using thread?
Harsh1  Tuesday, May 29, 2007 9:02 AM

Hi Harsh,

Try like this:

Code Snippet

Class Form1

Dim t As System.Threading.Thread = Nothing

Dim now As DateTime

Delegate Sub SetTextCallback(ByVal [text] As String)

Private Sub SetText(ByVal [text] As String)

If Me.Label1.InvokeRequired Then

Dim d As New SetTextCallback(AddressOf SetText)

Me.Invoke(d, New Object() {[text]})

Else

Me.Label1.Text = text

End If

End Sub

Private Sub showtime()

While True

System.Threading.Thread.Sleep(1000)

now = now.AddSeconds(1)

SetText(now.ToString("yyyy-MM-dd hh:mm:ss"))

End While

End Sub

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

now = DateTime.Now

t = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf showtime))

t.Start()

End Sub

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

t.Abort()

End Sub

Class

Hope this helps.

Regards

Hi Harsh,

Check this topic for more information about the three timer.

Comparing the Timer Classes in the .NET Framework Class Library

Hope this helps.

Regards

Rong-Chun Zhang  Tuesday, June 05, 2007 5:34 AM
Please tell me how to use thread instead of timer control for above purpose.
Harsh1  Wednesday, May 30, 2007 7:36 AM
Atleast tell me how to call a function repeatedly after one second byusing "thread"
Harsh1  Thursday, May 31, 2007 9:04 AM

Hi Harsh,

Try like this:

Code Snippet

Class Form1

Dim t As System.Threading.Thread = Nothing

Dim now As DateTime

Delegate Sub SetTextCallback(ByVal [text] As String)

Private Sub SetText(ByVal [text] As String)

If Me.Label1.InvokeRequired Then

Dim d As New SetTextCallback(AddressOf SetText)

Me.Invoke(d, New Object() {[text]})

Else

Me.Label1.Text = text

End If

End Sub

Private Sub showtime()

While True

System.Threading.Thread.Sleep(1000)

now = now.AddSeconds(1)

SetText(now.ToString("yyyy-MM-dd hh:mm:ss"))

End While

End Sub

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

now = DateTime.Now

t = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf showtime))

t.Start()

End Sub

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

t.Abort()

End Sub

Class

Hope this helps.

Regards

I'm still wondering exactly which Timer you are using: System.Threading, System.Windows.Forms or System.Timers

timvw  Friday, June 01, 2007 5:46 AM

Thanks a lot my problem is solved.

Now can u please tell me exactly what is the difference between timer(the timer control in toolbar of form's design view)and thread.

Previously I was using timer control to do the same thing.

I only know that thread is light weight process.

Any other advantages of using thread over timer.

Harsh1  Tuesday, June 05, 2007 5:20 AM

Hi Harsh,

Check this topic for more information about the three timer.

Comparing the Timer Classes in the .NET Framework Class Library

Hope this helps.

Regards

Rong-Chun Zhang  Tuesday, June 05, 2007 5:34 AM

You can use google to search for other answers

Custom Search

More Threads

• Can Toolstrip inherited to be changed?
• How to delete "zombie" controls in the designer
• Toolbar Button Problems...
• Visual Studio 2005 Reinstallation Critical problem
• Hosted controls on UserControl invisible
• problem in movement of image,form background colour occur
• DataGridViewColumn
• Forms designer hanging
• Report Filtering C# 2005 - rdlc report
• Adding Attributes at runtime to own class