Adding a timer to my C# Console app

Posted on 2006-11-29
Last Modified: 2012-06-21
Hi all,

I urgently need to add a timer to my Console app. I want my routine(Get_XML_Data) to run every 5 mins

Question by:MSAIT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Assisted Solution

AlexNek earned 70 total points
ID: 18036180
You an use hidden window or you can use additional timer thread where you can check time yourself

Accepted Solution

sasapopovic earned 180 total points
ID: 18036225
Hello MSAIT,

Here is how you can do it:

using System;
using System.Globalization;
using System.Text;

using Tradal.Common.Util;

namespace ConsoleApplication1
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
                  Console.WriteLine("Starting with processing...");
                  MyClass myClass = new MyClass();
                  Console.WriteLine("Press <ENTER> to exit");


      class MyClass
            System.Timers.Timer timer = null;

            public MyClass()
                  timer = new System.Timers.Timer();
                  timer.Interval = 300000;//5 minutes in miliseconds
                  timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

            public void StartProcessing()

            public void StopProcessing()

            private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

            private void Get_XML_Data()
                  //TODO: implement your logic here.

I hope this will help you.


Author Comment

ID: 18036339
Thanks guys,

I found this on the net

 1:  //  Timer02.cs - Displaying Date and Time
 2:  //      Using the Timer class.
 3:  //      Press Ctrl+C to end program.
 4:  //------------------------------------------
 5:  using System;
 6:  using System.Timers;
 8:  class myApp
 9:  {
10:    public static void Main()
11:    {
12:      Timer myTimer = new Timer();
13:      myTimer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
14:      myTimer.Interval = 1000;
15:      myTimer.Start();
17:      while ( Console.Read() != 'q' )
18:      {
19:          ;    // do nothing...
20:      }
21:    }
23:    public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
24:    {
25:        Console.Write("\r{0}", DateTime.Now);
26:    }
27:  }

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question