请问能否让某个线程定时运行一次?我希望是用线程本身的机制,而非另加一个定时器。(50分)

  • 主题发起人 lkdbdlkq
  • 开始时间
L

lkdbdlkq

Unregistered / Unconfirmed
GUEST, unregistred user!
比如说每天12:00的时候开始运行一次。
 
你需要的就是定时的功能,你不加定时器你就自己写个有定时功能的线程
对这种问题 真是无语。。。。。
 
线程中有专用的定时API函数的
 
定时器底层不是线程?
 
在线程中waitforsingleobject等候自己的句柄,设置好超时时间.
没到超时时间就是在休眠状态,到了时间会被自动激活.
线程代码这样写
While Truedo
begin
waitforsingleobject(GetCurrentThread(), 你的超时时间);
做你要做的事
end;
 
wr960204,正解!~
 
如果想实现不管啥时候启动程序,都是定时在某个时间启动线程,给你个办法:
function waitTimerProc(const p: pointer): integer;
begin
if (WaitForSingleObject(Cardinal(p), INFINITE) = WAIT_OBJECT_0) then
begin
{...你的线程执行代码}
end;
end;

procedure start;
var
st: SYSTEMTIME;
ft: FILETIME;
due: int64;
id: Cardinal;
hWaitTimer: Cardinal;
begin
// 可定时等待对象
hWaitTimer := CreateWaitableTimerA(nil, False, nil);
GetLocalTime(st);
st.wHour := 12;
// 12点0分
st.wMinute := 0;
SystemTimeToFileTime(st, ft);
LocalFileTimeToFileTime(ft, ft);
large_integer(due).LowPart := ft.dwLowDateTime;
large_integer(due).HighPart := ft.dwHighDateTime;
// 每24小时执行一次
SetWaitableTimer(hWaitTimer, due, 24 * 60 * 60 * 1000, nil, nil, False);
begin
Thread(nil, 0, @waitTimerProc, pointer(hWaitTimer), 0, id);
end;
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
961
SUNSTONE的Delphi笔记
S
S
回复
0
查看
950
SUNSTONE的Delphi笔记
S
顶部