我创建了一个线程,此线程内想周期调用一函数,而且必须向处理函数中传递线程内的一参数,SetTimer的函数原型是
UINT_PTR SetTimer(
  HWND hWnd,              // 窗口句柄
  UINT_PTR nIDEvent,      // 定时器ID,多个定时器时,可以通过该ID判断是哪个定时器
  UINT uElapse,           // 时间间隔,单位为毫秒
  TIMERPROC lpTimerFunc   // 回调函数
);毁掉函数的参数类型是不是已经定死了,那我该如何向函数中传递自己的参数,希望大家多多赐教!
疑惑阿,焦急等待。

解决方案 »

  1.   

    你可以在回调函数里面向线程发送一个自定义消息#include "stdafx.h"
    #include <windows.h>
    #define WM_MYMESSAGE WM_USER+1DWORD WINAPI ThreadFun(LPVOID wParam);int main(int argc, char* argv[])
    {
    HANDLE hThread=CreateThread(NULL,0,ThreadFun,NULL,0,NULL);
    WaitForSingleObject(hThread,INFINITE);
    return 0;
    }
    VOID CALLBACK TimerProc(
    HWND hwnd,         // handle to window
    UINT uMsg,         // WM_TIMER message
    UINT idEvent,  // timer identifier
    DWORD dwTime       // current system time
    )
    {
    PostThreadMessage(GetCurrentThreadId(),WM_MYMESSAGE,0,0);
    }void MyProc(int i)
    {
    printf("%d\n",i);
    }DWORD WINAPI ThreadFun(LPVOID wParam)
    {
    MSG msg;
    int i=0;
    SetTimer(NULL,NULL,1000,TimerProc);
    while(GetMessage(&msg,NULL,0,0))
    {
    switch(msg.message)
    {
    case WM_MYMESSAGE:
    i++;
    MyProc(i);
    break;
    default:
    DispatchMessage(&msg);
    }
    }
    return 0;
    }
      

  2.   

    乱了,再贴一遍
    #include "stdafx.h"
    #include <windows.h>
    #define WM_MYMESSAGE WM_USER+1DWORD WINAPI ThreadFun(LPVOID wParam);int main(int argc, char* argv[])
    {
    HANDLE hThread=CreateThread(NULL,0,ThreadFun,NULL,0,NULL);
    WaitForSingleObject(hThread,INFINITE);
    return 0;
    }
    VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
    {
    PostThreadMessage(GetCurrentThreadId(),WM_MYMESSAGE,0,0);
    }void MyProc(int i)
    {
    printf("%d\n",i);
    }DWORD WINAPI ThreadFun(LPVOID wParam)
    {
    MSG msg;
    int i=0;
    SetTimer(NULL,NULL,1000,TimerProc);
    while(GetMessage(&msg,NULL,0,0))
    {
    switch(msg.message)
    {
    case WM_MYMESSAGE:
    i++;
    MyProc(i);
    break;
    default:
    DispatchMessage(&msg);
    }
    }
    return 0;
    }
      

  3.   

    嗬嗬,谢谢哈哈,你的方法是可行的,不过考虑到精度问题,后来用了waitabletimer控件,问题解决,thank you all the same^_^