ARTICLE

Timers sample - Two timers working in tandem

Posted by Rahul Chadha Articles | Windows Controls June 07, 2005
The sample uses two timers and demonstrates how to tame them.
Download Files:
 
Reader Level:

I have seen people getting confused by timers. As each timer launches a new thread, the problem worsens if one has more than one of them on the same. I am pleased to share the sample solution attached, with the community. The sample uses two timers and demonstrates how to tame them.

There are pictures of three faces (happy, neutral, and sad) on the form. The sample uses two timers - DisplayTimer to display each face one by one, and HideTimer to hide each face one by one. The timers keep doing their work until the user clicks the Exit button. The two timers besides working in their own thread, work mutually exclusive of each other. The code of the sample is well - commented and follows general naming convention.

Following are the points to note about the sample:

Constraints:

  1. A timer always starts a new thread.

Business logic:

  1. Display the faces one by one.
  2. Unless all the faces are displayed completely, don't start hiding them.
  3. Hide the faces one by one.
  4. Unless all the faces are hidden, don't start displaying them again.
  5. Even if more faces (images) are added on the form later, we shouldn't need to change the code.

Optimization:

  1. No controls are added on the form at run time. Therefore, the sample retrieves the index of each PictureBox in the controls collection, and stores it in a local array. Thus, we don't need to loop thru the Controls collection repeatedly.

How to read the Program

Display and Hide timers need to be mutually exclusive. The sample uses some form level permission variables to achieve this.

  1. Take look at the region "Form Level Variables" first.
  2. Then the Sub Form_Load().
  3. Then Sub Display_Tick(), and Sub Hide_Tick(). These are almost alike.
  4. There are some simple try Catch blocks in place.
  5. There is some cleaning code in the btnExit_click().

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor