Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
1.57/5 (4 votes)
See more:
Dear All,
I am using visual studio 2010 and c# language.I Need to use Timer Control in Console Application I am using Following Code but ll Not work out. Any one Give me the Solution.


C#
static void Main(string[] args)
        {
            Timer myTime = new Timer();
            myTime.Interval = 100;
            myTime.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
            myTime.Start();         
        }

        public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
        {            
            tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
            Int32 IdleTime;
            LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
            LastInput.dwTime = 0;

            if (GetLastInputInfo(ref LastInput))
            {
                IdleTime = System.Environment.TickCount - LastInput.dwTime;
            }
        }


Thanks Advance!..
Posted
Updated 26-May-21 11:09am
v2
Comments
F-ES Sitecore 29-Sep-15 5:30am    
Define "not work"? We don't know the problem so how can we give you a solution?
phil.o 29-Sep-15 8:34am    
The solution to what?

Try this:

C#
using System;
using System.Threading;

public static class Program
{

    public static void Main()
    {
        // Create a Timer object that knows to call our DisplayTimeEvent
        // method once every 100 milliseconds.
        Timer t = new Timer(DisplayTimeEvent, null, 0, 100);

        // Wait for the user to hit <Enter>
        Console.ReadLine();
    }

    private static void DisplayTimeEvent(Object o)
    {
        // Display the date/time when this method got called.
        Console.WriteLine("In TimerCallback: " + DateTime.Now);

        // Force a garbage collection to occur for this demo.
        //GC.Collect();
    }
}
 
Share this answer
 
v4
Comments
CPallini 29-Sep-15 5:54am    
5.By the way, why are you collecting garbage inside the callback?
Leo Chapiro 29-Sep-15 6:14am    
A good question! This example is from this book of Jeffrey Richter: https://www.microsoftpressstore.com/articles/article.aspx?p=2224054 The author will explain "Garbage Collections and Debugging":

Compile this code from the command prompt without using any special compiler switches. When you run the resulting executable file, you’ll see that the TimerCallback method is called just once!

From examining the preceding code, you’d think that the TimerCallback method would get called once every 2,000 milliseconds. After all, a Timer object is created, and the variable t refers to this object. As long as the timer object exists, the timer should keep firing. But you’ll notice in the TimerCallback method that I force a garbage collection to occur by calling GC.Collect().

When the collection starts, it first assumes that all objects in the heap are unreachable (garbage); this includes the Timer object. Then, the collector examines the application’s roots and sees that Main doesn’t use the t variable after the initial assignment to it. Therefore, the application has no variable referring to the Timer object, and the garbage collection reclaims the memory for it; this stops the timer and explains why the TimerCallback method is called just once.
Ok - Here you have a Main thread that starts when the app starts. The Main thread will continue until it reaches an exit condition (usually the end of the code).

Any other thread running will be aborted when the Main thread completes.

You are starting your timer thread mere moments before the main thread exits.

For a console app, I would suggest adding a Console.ReadLine() after myTime.Start(). The readline command will make the Main thread wait for an input allowing the timer thread to execute.


Some other things to look into are:

Event Synchronization: Using events for thread synchronization[^]
This is useful when you want automated triggers to stop and start threads.

Windows Services:Creating a simple Windows Service[^]
If you ever want an app running that always has a thread ticking over, even a timer (which is often used in services), then consider using a windows service. It's designed to sit in the background and constantly process threads.

I hope that helps ^_^

Andy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900