Re: need to add clock to application
Dave <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <000001c77deb$3d6f4f40$6400a8c0@omniscient1> |
I did it in my application using a few lines of code First, you need another thread. The best is to create a delegate, point it to your updater function (UpdateTime, maybe), and call "BeginInvoke". Then, your class containing the UpdateTime function must have a reference to the "view" (the form containing the status bar... Use an interface here, it's not good design to pass the form itself) The UpdateTime function will loop indefinitely, and sleep for 499 milliseconds between each loop. The loop then has two lines of code: Do m_statusView.UpdateTime(now) Threading.Thread.Sleep(499) While m_continue = True And here's the trick: the form implementing the IStatusView interface (kept I the m_statusView variable) MUST update the status bar in the SAME THREAD it's been created with. That means the code will crash if you don't do this: call Me.Invoke(AddressOf UpdateStatus) That's a pretty nice trick: the implementation of Control.Invoke automatically switches the thread to the thread that created the control (here, the Form). You will then be able to update the text of the status bar!! Works pretty well, with no flicker at all. Have fun, Dave. www.omniscient.ca www.omniscienttrader.com -----Original Message----- From: Discussion forum for developers using Windows Forms to build apps and controls [mailto:[email protected]] On Behalf Of Stacey Levine Sent: April 12, 2007 12:30 PM To: [email protected] Subject: [DOTNET-WINFORMS] need to add clock to application I need to add a clock to the status bar in my application. It certainly seems simple to add a timer and update the display every 10 seconds or so.. but that seems so inelegant. Does anyone have any better ideas? Is there a component that people have used? Thanks. Stacey