zul ajax使用线程池

本文介绍了一种利用线程池定时器功能,在指定时间内未收到用户响应时自动关闭消息框的方法。通过全局变量设置响应时间,并在超时时更新消息框内容。实现了用户交互与后台任务的高效协同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能说明: 使用线程池的定时器函数来实现一个用户在规定时间内不作出响应时能自动关闭的消息框。

当启动该程序时,它将全局变量g _ n S e c L e f t设置为1 0。这表示用户必须在规定时间内对消息框作出响应的秒数。然后调用C r e a t e Ti m e r Q u e u e Ti m e r函数,指令线程池每秒钟调用一次M s g B o x Ti m e o u t函数。

作为线程池使用的范例,同时代码可以修改后重用。
附有完整的工程和编译好的程序。


/******************************************************************************
Module: TimedMsgBox.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/


#include "..\CmnHdr.h" /* See Appendix A. */
#include <tchar.h>


//////////////////////////////////////////////////////////////////////////////


// The caption of our message box
TCHAR g_szCaption[] = TEXT("Timed Message Box");


// How many seconds we'll display the message box
int g_nSecLeft = 0;


// This is STATIC window control ID for a message box
#define ID_MSGBOX_STATIC_TEXT 0x0000ffff


//////////////////////////////////////////////////////////////////////////////


VOID WINAPI MsgBoxTimeout(PVOID pvContext, BOOLEAN fTimeout) {

// NOTE: Due to a thread race condition, it is possible (but very unlikely)
// that the message box will not be created when we get here.
HWND hwnd = FindWindow(NULL, g_szCaption);

if (hwnd != NULL) {
// The window does exist; update the time remaining.
TCHAR sz[100];
wsprintf(sz, TEXT("You have %d seconds to respond"), g_nSecLeft--);
SetDlgItemText(hwnd, ID_MSGBOX_STATIC_TEXT, sz);

if (g_nSecLeft == 0) {
// The time is up; force the message box to exit.
EndDialog(hwnd, IDOK);
}
} else {

// The window does not exist yet; do nothing this time.
// We'll try again in another second.
}
}


//////////////////////////////////////////////////////////////////////////////


int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {

chWindows9xNotAllowed();

// How many seconds we'll give the user to respond
g_nSecLeft = 10;

// Create a multishot 1 second timer that begins firing after 1 second.
HANDLE hTimerQTimer;
CreateTimerQueueTimer(&hTimerQTimer, NULL, MsgBoxTimeout, NULL,
1000, 1000, 0);

// Display the message box
MessageBox(NULL, TEXT("You have 10 seconds to respond"),
g_szCaption, MB_OK);

// Cancel the timer & delete the timer queue
DeleteTimerQueueTimer(NULL, hTimerQTimer, NULL);

// Let us know if the user responded or if we timed-out.
MessageBox(NULL,
(g_nSecLeft == 0) ? TEXT("Timeout") : TEXT("User responded"),
TEXT("Result"), MB_OK);

return(0);
}


//////////////////////////////// End of File /////////////////////////////////
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值