Solved

Adding a timer to my C# Console app

Posted on 2006-11-29
3
1,089 Views
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

thanks
0
Comment
Question by:MSAIT
3 Comments
 
LVL 16

Assisted Solution

by:AlexNek
AlexNek earned 70 total points
Comment Utility
You an use hidden window or you can use additional timer thread where you can check time yourself
http://technology.chtsai.org/w98timer/
0
 
LVL 9

Accepted Solution

by:
sasapopovic earned 180 total points
Comment Utility
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>
            [STAThread]
            static void Main(string[] args)
            {
                  Console.WriteLine("Starting with processing...");
                  MyClass myClass = new MyClass();
                  myClass.StartProcessing();
                  
                  Console.WriteLine("Press <ENTER> to exit");
                  Console.ReadLine();

                  myClass.StopProcessing();
            }
      }

      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()
            {
                  timer.Start();
            }

            public void StopProcessing()
            {
                  timer.Stop();
            }

            private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                  Get_XML_Data();
            }

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


I hope this will help you.

Regards,
Sasa
0
 
LVL 2

Author Comment

by:MSAIT
Comment Utility
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;
 7:
 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();
16:
17:      while ( Console.Read() != 'q' )
18:      {
19:          ;    // do nothing...
20:      }
21:    }
22:
23:    public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
24:    {
25:        Console.Write("\r{0}", DateTime.Now);
26:    }
27:  }
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now