简体   繁体   English

在其他线程上创建计时器-没有回调函数(C,Windows)

[英]Create a timer on a different thread - with no callback function (C, Windows)

Is there a way to create a timer (say, to 10 seconds) on a different thread? 有没有一种方法可以在其他线程上创建计时器(例如10秒)? I mean, I know how to use CreateThread() and I know how to create/use timers. 我的意思是,我知道如何使用CreateThread(),也知道如何创建/使用计时器。 The problem I have is that the new thread cannot receive a callback function. 我的问题是新线程无法接收回调函数。

For those that will inevitably ask "why do you want to do this?" 对于那些不可避免地会问“为什么要这样做”的人? the answer is because i have to do it this way. 答案是因为我必须这样做。 it is part of a bigger program that can't at this specific part of the code use callback functions. 它是一个较大程序的一部分,该程序的特定部分不能使用回调函数。 that's all. 就这样。

Is there any way to achieve this? 有什么办法可以做到这一点?

code is appreciated. 代码表示赞赏。

Thanks! 谢谢!

EDIT: 编辑:

A better explanation of the problem: My application consist of two separate programs. 对该问题的更好解释:我的应用程序包含两个单独的程序。 The main program (visible, interface for the user) and another doing the hard work in the background (sort of like a daemon). 主程序(可见的用户界面)和另一个在后台进行艰苦工作的程序(有点像守护程序)。 The background process need to finishing writing to the DB and closing a lot of little files before exiting. 在退出之前,后台进程需要完成对数据库的写入并关闭许多小文件。 The main application send a "we're done" message to that background process. 主应用程序向该后台进程发送“我们已完成”消息。 Upon receiving this the background process returns the current status and exists. 收到此消息后,后台进程将返回当前状态并存在。 Now, I need to add the following: upon receiving the message it returns a status and triggers a timer that will wait X amount of time on another thread, in the meantime the background process closes all the DB connections and files. 现在,我需要添加以下内容:收到消息后,它返回一个状态并触发一个计时器,该计时器将在另一个线程上等待X的时间,与此同时,后台进程将关闭所有数据库连接和文件。 If the timer reached 0 then and the background process is still alive then it terminates it. 如果计时器达到0,则后台进程仍处于活动状态,则它将终止。 If the background process closed all the db and files then the thread (and timer) will die before reaching 0 as the application terminates normally. 如果后台进程关闭了所有数据库和文件,则线程(和计时器)将在达到0之前消失,因为应用程序正常终止。

Is this better? 这是否更好?

So, you need a watchdog inside the DB process (I misread again, didn't I). 因此,您需要在数据库进程中使用看门狗(我又读错了,不是)。 ThreadProc like this will probably suffice, since all threads terminates when main thread terminates: 这样的ThreadProc可能就足够了,因为所有线程都在主线程终止时终止:

DWORD WINAPI TerminateAfter10s(LPVOID param) {
 Sleep(10000);
 ExitProcess(0);
}

If you use the multimedia timer function timeSetEvent, it can be configured to pulse an event rather than use the normal callback. 如果使用多媒体计时器功能timeSetEvent,则可以将其配置为使事件产生脉冲而不是使用常规回调。 Does that satisfy the requirement ? 满足要求吗?

I'm more interested in knowing why you have this requirement to avoid the use of a callback. 我对知道为什么要避免使用回调有这个要求更感兴趣。 Callbacks would seem to be entirely appropriate to use in a worker thread. 回调似乎完全适合在工作线程中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM