没有窗口,如何SetTimer

谁学逆向工程 2014-08-05 08:47:40
CreateThread一个线程,用于后台定时执行某些工作。既然是后台当然就没有HWND,没有HWND就没法使用SetTimer。除了在循环中使用Sleep还有什么方法
...全文
360 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
SetTimer处理 回调函数即可,若果没句柄。 句柄是方便在执行一个时间事件以定时器ID做标志。
zhousitiaoda 2014-08-05
  • 打赏
  • 举报
回复
SetTimer跟HWnd没有关系,但跟消息循环有关系,如果是一个工作线程,是不能使用定时器的,你必须调用GetMessage类似的API来产生一个消息循环,举个例子

DWORD CALLBACK CManager::CheckProc(LPVOID lpParameter)
{
	CManager* pThis = (CManager*)lpParameter;
	if (NULL == pThis)
	{
		return 0;
	}

	BOOL bStartedTimer = FALSE;
	int nTimerID = 0;
	DWORD dwRecvTime = 0;
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0))
	{
		switch(msg.message)
		{
		case WM_THREAD_RECV_HEART_BEAT:
			{
				nTimerID = SetTimer(NULL, 1, 1, NULL);
			}
			break;

		case WM_TIMER:
			{
				if (nTimerID == msg.wParam)
				{
					
				}
			}
			break;
		}

		TranslateMessage(&msg);
		DispatchMessage(&msg); 
	}

	return 0;
}
这就是一个线程函数,这里我是在外部给线程发送一个消息(通过postthreadmessage),在消息响应函数里创建定时器。
叶恭介叶恭介 2014-08-05
  • 打赏
  • 举报
回复
UINT_PTR SetTimer( HWND hWnd, // 窗口句柄 UINT_PTR nIDEvent, // 定时器ID,多个定时器时,可以通过该ID判断是哪个定时器 UINT nElapse, // 时间间隔,单位为毫秒 TIMERPROC lpTimerFunc // 回调函数 ); 返回值: 类型:UINT_PTR 如果函数成功,hWnd参数为0,则返回新建立的时钟编号,可以把这个时钟编号传递给KillTimer来销毁时钟. 如果函数成功,hWnd参数为非0,则返回一个非零的整数,可以把这个非零的整数传递给KillTimer来销毁时钟. 如果函数失败,返回值是零.若想获得更多的错误信息,调用GetLastError函数. Setimer没说一定要Hwnd.TIMERPROC lpTimerFunc // 回调函数应该可用调用吧。你试试,我也没试过
fishion 2014-08-05
  • 打赏
  • 举报
回复
可以WaitForSingleObject一个事件,填上时间,或者就是CreateWaitableTimer等
赵4老师 2014-08-05
  • 打赏
  • 举报
回复
#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
    struct tm *now;
    struct timeb tb;

    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,s);
}
VOID CALLBACK myTimerProc1(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc2\n");
}
int main() {
    int i;
    MSG msg;

    SetTimer(NULL,0,1000,myTimerProc1);
    SetTimer(NULL,0,2000,myTimerProc2);
    for (i=0;i<20;i++) {
        Sleep(500);
        log("In main\n");
        if (GetMessage(&msg,NULL,0,0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

    }
    return 0;
}
//2012-07-26 17:29:06.375 In main
//2012-07-26 17:29:06.875 In myTimerProc1
//2012-07-26 17:29:07.375 In main
//2012-07-26 17:29:07.875 In myTimerProc2
//2012-07-26 17:29:08.375 In main
//2012-07-26 17:29:08.375 In myTimerProc1
//2012-07-26 17:29:08.875 In main
//2012-07-26 17:29:08.875 In myTimerProc1
//2012-07-26 17:29:09.375 In main
//2012-07-26 17:29:09.890 In myTimerProc2
//2012-07-26 17:29:10.390 In main
//2012-07-26 17:29:10.390 In myTimerProc1
//2012-07-26 17:29:10.890 In main
//2012-07-26 17:29:10.890 In myTimerProc1
//2012-07-26 17:29:11.390 In main
//2012-07-26 17:29:11.890 In myTimerProc2
//2012-07-26 17:29:12.390 In main
//2012-07-26 17:29:12.390 In myTimerProc1
//2012-07-26 17:29:12.890 In main
//2012-07-26 17:29:12.890 In myTimerProc1
//2012-07-26 17:29:13.390 In main
//2012-07-26 17:29:13.890 In myTimerProc2
//2012-07-26 17:29:14.390 In main
//2012-07-26 17:29:14.390 In myTimerProc1
//2012-07-26 17:29:14.890 In main
//2012-07-26 17:29:14.890 In myTimerProc1
//2012-07-26 17:29:15.390 In main
//2012-07-26 17:29:15.890 In myTimerProc2
//2012-07-26 17:29:16.390 In main
//2012-07-26 17:29:16.390 In myTimerProc1
//2012-07-26 17:29:16.890 In main
//2012-07-26 17:29:16.890 In myTimerProc1
//2012-07-26 17:29:17.390 In main
//2012-07-26 17:29:17.890 In myTimerProc2
//2012-07-26 17:29:18.390 In main
//2012-07-26 17:29:18.390 In myTimerProc1
//2012-07-26 17:29:18.890 In main
//2012-07-26 17:29:18.890 In myTimerProc1
//2012-07-26 17:29:19.390 In main
//2012-07-26 17:29:19.890 In myTimerProc2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
    #include <process.h>
    #define  MYVOID             void
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
    #define  MYVOID             void *
#endif
//Log{
#define MAXLOGSIZE 20000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
void sleep_ms(int ms) {
    Sleep(ms);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
void sleep_ms(int ms) {
    usleep(ms*1000);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;


    if (NULL==pszFmt||0==pszFmt[0]) return;
    if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
            flog=fopen(logfilename1,"a");
            if (NULL==flog) return;
        }
        fclose(flog);
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int No_Loop=0;
MYVOID testThread(void *pcn) {
    int n,i;

    n=(int)pcn;
    i=0;
    while (1) {
        sleep_ms(1000);
        Log("in testThread %d:i==%ds\n",n,++i);
        if (i>=5) No_Loop=1;
    }
}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
    pthread_t threads[1];
    int threadsN;
    int rc;
#endif
    Log("=========BEGIN==================\n");
#ifdef WIN32
    _beginthread((void(__cdecl *)(void *))testThread,0,(void *)1);
#else
    threadsN=0;
    rc=pthread_create(&(threads[threadsN++]),NULL,testThread,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
#endif
    i=0;
    while (1) {
        sleep_ms(100);
        Log("in main:i==%d\n",++i);
        if (No_Loop==1) break;//
    }
    Log("=========END====================\n");
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//2012-06-14 16:27:21.500 =========BEGIN==================
//2012-06-14 16:27:21.609 in main:i==1
//2012-06-14 16:27:21.718 in main:i==2
//2012-06-14 16:27:21.828 in main:i==3
//2012-06-14 16:27:21.937 in main:i==4
//2012-06-14 16:27:22.046 in main:i==5
//2012-06-14 16:27:22.156 in main:i==6
//2012-06-14 16:27:22.265 in main:i==7
//2012-06-14 16:27:22.375 in main:i==8
//2012-06-14 16:27:22.484 in main:i==9
//2012-06-14 16:27:22.500 in testThread 1:i==1s
//2012-06-14 16:27:22.593 in main:i==10
//2012-06-14 16:27:22.703 in main:i==11
//2012-06-14 16:27:22.812 in main:i==12
//2012-06-14 16:27:22.921 in main:i==13
//2012-06-14 16:27:23.031 in main:i==14
//2012-06-14 16:27:23.140 in main:i==15
//2012-06-14 16:27:23.250 in main:i==16
//2012-06-14 16:27:23.359 in main:i==17
//2012-06-14 16:27:23.468 in main:i==18
//2012-06-14 16:27:23.500 in testThread 1:i==2s
//2012-06-14 16:27:23.578 in main:i==19
//2012-06-14 16:27:23.687 in main:i==20
//2012-06-14 16:27:23.796 in main:i==21
//2012-06-14 16:27:23.906 in main:i==22
//2012-06-14 16:27:24.015 in main:i==23
//2012-06-14 16:27:24.125 in main:i==24
//2012-06-14 16:27:24.234 in main:i==25
//2012-06-14 16:27:24.343 in main:i==26
//2012-06-14 16:27:24.453 in main:i==27
//2012-06-14 16:27:24.500 in testThread 1:i==3s
//2012-06-14 16:27:24.562 in main:i==28
//2012-06-14 16:27:24.671 in main:i==29
//2012-06-14 16:27:24.781 in main:i==30
//2012-06-14 16:27:24.890 in main:i==31
//2012-06-14 16:27:25.000 in main:i==32
//2012-06-14 16:27:25.109 in main:i==33
//2012-06-14 16:27:25.218 in main:i==34
//2012-06-14 16:27:25.328 in main:i==35
//2012-06-14 16:27:25.437 in main:i==36
//2012-06-14 16:27:25.500 in testThread 1:i==4s
//2012-06-14 16:27:25.546 in main:i==37
//2012-06-14 16:27:25.656 in main:i==38
//2012-06-14 16:27:25.765 in main:i==39
//2012-06-14 16:27:25.875 in main:i==40
//2012-06-14 16:27:25.984 in main:i==41
//2012-06-14 16:27:26.093 in main:i==42
//2012-06-14 16:27:26.203 in main:i==43
//2012-06-14 16:27:26.312 in main:i==44
//2012-06-14 16:27:26.421 in main:i==45
//2012-06-14 16:27:26.500 in testThread 1:i==5s
//2012-06-14 16:27:26.531 in main:i==46
//2012-06-14 16:27:26.531 =========END====================

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧