如何在控制台应用程序中使用Timer Control? [英] How to use Timer Control in Console Application?

查看:243
本文介绍了如何在控制台应用程序中使用Timer Control?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在使用visual studio 2010和c#语言。我需要在控制台应用程序中使用Timer Control我正在使用以下代码但是没有用完。任何一个给我解决方案。





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.


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!..

推荐答案

试试这个:



Try this:

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();
    }
}


确定 - 这里有一个主要线程,它在应用程序启动时启动。主线程将一直持续到达退出状态(通常是代码结束)。



当主线程完成时,任何其他运行的线程都将被中止。 br />


你在主线程退出之前就开始你的计时器线程。



对于控制台应用程序,我建议在myTime.Start()之后添加一个Console.ReadLine()。 readline命令将使主线程等待允许计时器线程执行的输入。





其他一些需要考虑的事项是:



事件同步:使用事件进行线程同步 [ ^ ]

当你想要自动触发器来停止和启动线程时这很有用。



Windows服务:创建简单的Windows服务 [ ^ ]

如果你想要一个应用程序运行总是有一个线程滴答,甚至一个计时器(通常在服务中使用),th考虑使用Windows服务。它被设计成坐在后台并不断处理线程。



我希望有所帮助^ _ ^



安迪
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


这篇关于如何在控制台应用程序中使用Timer Control?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆