Continuously Update Form  
Author Message
Shane_Jordan





PostPosted: Visual Basic Express Edition, Continuously Update Form Top

I am creating a form that is going to act as a notification system for users. It has 8 buttons on the form, each button represents a location. Based on the current status of the location, the buttons color will change. I have the program reading from a database at startup and updating the buttons color.

What I am needing is how do I have the form check the database after it has been loaded to see if it needs to update the color of a button. What I am planning on doing is, using Thread.Sleep to make the program sleep for about 5 minutes, then check the database again. This would be in an infinite loop, so that the program will always run and update the color of the buttons.

I tried to use the Load event, but that does not load the form before it goes to sleep. Is there an event that I can use to accomplish this task



Visual Studio Express Editions31  
 
 
spotty





PostPosted: Visual Basic Express Edition, Continuously Update Form Top

Absolutely do not use thread sleep for 5 minutes.   This will make the application be completely dead for 5 minutes,  you wont be able to click on close or anything in that time.

Put a timer on the form and set its duration to 5 minutes and put you code (or call your code) from the timer tick event.

This way every 5 minutes the application will Poll the database and retrieve the data but you application is still alive to do other thing in the meantime - such as click the close button to close the appliaction.

Thread.sleep should only be used for very small durations ( a second or two) after that start looking at things like timers.   You need to forget about writing infinite loops etc. and start thinking events.   As VB.NET is event driven.

 

 


 
 
Shane_Jordan





PostPosted: Visual Basic Express Edition, Continuously Update Form Top

Where would I want to call the timer from, what type of event
 
 
ReneeC





PostPosted: Visual Basic Express Edition, Continuously Update Form Top

Declare

Protected Friend withevents tmr as New Timer ' Member variable

In the code editor in the upper left hand window select tmr

In the upper right hand window select Tick event

That will create an event handler in your source.

Intialize the timer in the form load

tmr.inteval = number of desired millisconds between ticks

tmr.start



 
 
spotty





PostPosted: Visual Basic Express Edition, Continuously Update Form Top

Or simply drop a timer control on the form, double click the control and enter your call in the tick event.

Then you simply need to set the timer control  interval property and call the timer control start method in your form load event.

 Every (duration) it will execute the tick event.