(原創) 如何使用Timer? (.NET) (C#) (C++/CLI)

C# / SimpleTimer.cs

/* 
(C) OOMusou 2007 
http://oomusou.cnblogs.com

Filename    : SimpleTimer.cs
Compiler    : Visual Studio 2005 / C# 2.0
Description : Simple demo for timer
Release     : 07/28/2007 1.0
*/

using System;
using System.Threading;

class MyClock {
  
public static void showTime(Object stateInfo) {
    Console.WriteLine(
"{0}", DateTime.Now.ToString("h:mm:ss:fff"));
  }

}

class Client {
  
static void Main() {
    Timer timer 
= new Timer(new TimerCallback(MyClock.showTime), new AutoResetEvent(false), 0250);
    
while(true);
  }

}


C++/CLI / SimpleTimer.cpp

/* 
(C) OOMusou 2006 
http://oomusou.cnblogs.com

Filename    : SimpleTimer.cpp
Compiler    : Visual C++ 8.0 / C++/CLI
Description : Simple demo for timer
Release     : 07/28/2007 1.0
*/

#include 
"stdafx.h"

using namespace System;
using namespace System::Threading;

ref class MyClock {
public:
  
static void showTime(Object^ stateInfo);
}
;

void MyClock::showTime(Object^ stateInfo) {
  Console::WriteLine(
"{0}", DateTime::Now.ToString("h:mm:ss:fff"));
}


int main() {
  Timer
^ timer = gcnew Timer(gcnew TimerCallback(MyClock::showTime), gcnew AutoResetEvent(false), 0250);
  
while(true);
}


See Also
(原創) 如何每间格一段时间就执行function? (Web) (JavaScript)

posted on 2007-07-28 09:11  真 OO无双  阅读(5265)  评论(0编辑  收藏  举报

导航