在Windows服务中如何使用计时器?

解决方案

System.Timers.Timer t =new System.Timers.Timer(200);//实例化Timer类,设置间隔时间为10000毫秒; 
t.Elapsed  =new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; 
t.AutoReset =true;//设置是执行一次(false)还是一直执行(true); 
t.Enabled =true;//是否执行System.Timers.Timer.Elapsed事件;

参考

http://www.cnblogs.com/virusswb/archive/2008/12/24/1361365.html

如果你想在Windows应用程序实现计时器和倒计时功能,可以使用Windows API计时器控件和多媒体定时器。 以下是一个简单的示例代码,演示了如何在Windows应用程序使用计时器控件和多媒体定时器实现计时器和倒计时一分钟的功能: ``` #include <windows.h> #include <mmsystem.h> #define TIMER_ID 1 #define TIMER_INTERVAL 1000 // 定时器间隔,单位为毫秒 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc = {0}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wc.lpszClassName = TEXT("TimerWindow"); RegisterClass(&wc); HWND hWnd = CreateWindow(TEXT("TimerWindow"), TEXT("Timer Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hTimerLabel, hCountdownLabel; static int elapsedSeconds = 0, remainingSeconds = 60; switch (message) { case WM_CREATE: // 创建计时器控件 hTimerLabel = CreateWindow(TEXT("Static"), TEXT("00:00"), WS_CHILD | WS_VISIBLE | SS_CENTER, 50, 50, 300, 50, hWnd, NULL, NULL, NULL); // 创建倒计时标签 hCountdownLabel = CreateWindow(TEXT("Static"), TEXT("倒计时:60秒"), WS_CHILD | WS_VISIBLE, 50, 120, 300, 50, hWnd, NULL, NULL, NULL); // 启动多媒体定时器 timeSetEvent(TIMER_INTERVAL, 0, (LPTIMECALLBACK) &TimerProc, (DWORD_PTR) hWnd, TIME_PERIODIC); break; case WM_TIMER: switch (wParam) { case TIMER_ID: // 更新计时器控件 elapsedSeconds++; TCHAR timerText[20]; _stprintf_s(timerText, TEXT("%02d:%02d"), elapsedSeconds / 60, elapsedSeconds % 60); SetWindowText(hTimerLabel, timerText); // 更新倒计时标签 remainingSeconds--; TCHAR countdownText[20]; _stprintf_s(countdownText, TEXT("倒计时:%d秒"), remainingSeconds); SetWindowText(hCountdownLabel, countdownText); if (remainingSeconds == 0) { // 倒计时结束,停止定时器 timeKillEvent(TIMER_ID); } break; } break; case WM_DESTROY: // 停止多媒体定时器 timeKillEvent(TIMER_ID); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } void CALLBACK TimerProc(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) { // 向窗口发送定时器消息 SendMessage((HWND) dwUser, WM_TIMER, TIMER_ID, 0); } ``` 该示例代码,创建了一个窗口,并在窗口创建了一个计时器控件和一个倒计时标签。通过调用timeSetEvent函数启动一个多媒体定时器,定时器每隔1秒钟向窗口发送一个WM_TIMER消息。窗口接收到WM_TIMER消息后,更新计时器控件和倒计时标签,直到倒计时结束。在窗口销毁时,调用timeKillEvent函数停止定时器。 注意,该示例代码只是一个简单的示例,实际应用需要考虑更多的细节和异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值