何时会触发OnTimer事件?

时间:2010-10-19 16:08:14

标签: visual-c++

任何人都可以告诉我何时会触发OnTimer事件? (VC ++新手) (抱歉格式错误)

enter code here

// iSCSIDlg.h:头文件

定义WM_RECVDATA WM_USER + 1

类CISCSIDlg:public CDialog { 上市:     bool m_testrand;     bool m_ack;     bool m_testProc;     BOOL m_isConnect;     BOOL InitSocket();     CISCSIDlg(CWnd * pParent = NULL);     static DWORD WINAPI RecvProc(LPVOID lpParameter);
    static DWORD WINAPI send_test(LPVOID lpParameter);

protected:
virtual void DoDataExchange(CDataExchange* pDX);

保护:     afx_msg void OnTimer(UINT nIDEvent);     DECLARE_MESSAGE_MAP()

};

类CAboutDlg:public CDialog { 上市:     CAboutDlg();     枚举{IDD = IDD_ABOUTBOX};     保护:     virtual void DoDataExchange(CDataExchange * pDX);

保护:     DECLARE_MESSAGE_MAP() };

CAboutDlg :: CAboutDlg():CDialog(CAboutDlg :: IDD) { }

void CAboutDlg :: DoDataExchange(CDataExchange * pDX) {     CDialog的:的DoDataExchange(PDX); }

BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)

END_MESSAGE_MAP()

CISCSIDlg :: CISCSIDlg(CWnd * pParent)     :CDialog(CISCSIDlg :: IDD,pParent) {     m_srvPort = 0;     m_cmd = -1;     m_param1 = 0;     m_hIcon = AfxGetApp() - > LoadIcon(IDR_MAINFRAME); }

void CISCSIDlg :: DoDataExchange(CDataExchange * pDX) {     CDialog的:的DoDataExchange(PDX);     DDX_Control(pDX,IDC_BTN_CONNECT,m_btnConnect);     DDX_Control(pDX,IDC_BTN_SEND,m_btnSend);     DDX_Text(pDX,IDC_EDIT_PORT,m_srvPort);     DDX_Radio(pDX,IDC_RADIO0,m_cmd);     DDX_Text(pDX,IDC_EDIT1,m_param1); }

BEGIN_MESSAGE_MAP(CISCSIDlg,CDialog)     ON_WM_SYSCOMMAND()     ON_WM_PAINT()     ON_WM_QUERYDRAGICON()     ON_BN_CLICKED(IDC_BTN_CONNECT,OnBtnConnect)     ON_BN_CLICKED(IDC_BTN_SEND,OnBtnSend)     ON_BN_CLICKED(IDC_RADIO0,OnRadio)     ON_WM_TIMER()     ON_MESSAGE(WM_RECVDATA,OnRecvData) END_MESSAGE_MAP()

BOOL CISCSIDlg :: OnInitDialog() {     CDialog的:的OnInitDialog();     CMenu * pSysMenu = GetSystemMenu(FALSE);     if(pSysMenu!= NULL)     {         CString strAboutMenu;         strAboutMenu.LoadString(IDS_ABOUTBOX);         if(!strAboutMenu.IsEmpty())         {             pSysMenu-> AppendMenu(MF_SEPARATOR);             pSysMenu-> AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);         }     }     的UpdateData(FALSE);     函数GetDlgItem(IDC_EDIT1) - >的ShowWindow(SW_HIDE);     函数GetDlgItem(IDC_EDIT2) - >的ShowWindow(SW_HIDE);     返回TRUE; }

void CISCSIDlg :: OnSysCommand(UINT nID,LPARAM lParam) {     if((nID& 0xFFF0)== IDM_ABOUTBOX)     {         CAboutDlg dlgAbout;         dlgAbout.DoModal();     }     其他     {         CDialog :: OnSysCommand(nID,lParam);     } }

void CISCSIDlg :: OnPaint() {

}

HCURSOR CISCSIDlg :: OnQueryDragIcon() {     返回(HCURSOR)m_hIcon; }

DWORD WINAPI CISCSIDlg :: RecvProc(LPVOID lpParameter) {     SOCKET袜子=((RECVPARAM *)lpParameter) - >袜子;     HWND hwnd =((RECVPARAM *)lpParameter) - > hwnd;     删除lpParameter;

unsigned char recvBuf[1500];
unsigned char tempBuf[1500];
int retval;
while(TRUE)
{
    memset(recvBuf,0,sizeof(recvBuf));
    retval=recv(sock,(char *)recvBuf,1500,0);
    memcpy(tempBuf,recvBuf,1500);
    ::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf);
}
return 0;

}

void CISCSIDlg :: OnRecvData(WPARAM wParam,LPARAM lParam) {

CTime time=CTime::GetCurrentTime();

if(msg[12]==0)
{

}
else
{

    send(m_socket,(char*)tmp_buf,32,0);
}

if(msg[12]==0 && m_testProc)
{

    SetDlgItemText(IDC_ERR,temp);
}
else
    if(msg[12]==0 && m_testrand)
    {

    }
    else
        if(m_testProc || m_testrand)
            m_ack=true;

}

void CISCSIDlg :: OnBtnConnect() {

}

void CISCSIDlg :: OnBtnSend() {

send(m_socket,(char*)msg_header,cmdSize+16,0);//kh 2

}

void CISCSIDlg :: OnTimer(UINT nIDEvent) {

if(nIDEvent==1)
{
    timeout--;
    if(timeout<0)
    {
        KillTimer(1);
        flag_test_ready=0;
        AfxMessageBox("xxx");
    }

}

if(nIDEvent==2)
{
    unsigned char testBuf[100];
    int testSize=16;
    memset(testBuf,0,sizeof(testBuf));

    testBuf[0]=0xa5;


    if(m_testrand  &&  m_isConnect )
    {
        if(m_ack)
        {
            m_ack=false;

            unsigned char msg_header[16+100];



            send(m_socket,(char*)msg_header,16+testSize,0);

            CString toShow,temp;
            CTime time=CTime::GetCurrentTime();

            toShow.Format("rand:%2d:%2d:%2d  cmd(%2x):--",time.GetHour(),time.GetMinute(),time.GetSecond(),testBuf[0]);
            for( i=0;i<testSize+16;i++)
            {
                temp.Format(" %2x",msg_header[i]);  
                toShow+=temp;
                if(i%8==7)
                {
                    ((CListBox*)GetDlgItem(IDC_LIST_MSG))->InsertString(0,toShow);
                    toShow.Empty();
                    toShow.Format("rand:%2d:%2d:%2d  cmd(%2x):--",time.GetHour(),time.GetMinute(),time.GetSecond(),testBuf[0]);
                }
            }
            if(i%8!=0)
                ((CListBox*)GetDlgItem(IDC_LIST_MSG))->InsertString(0,toShow);
        }
    }
}

CDialog::OnTimer(nIDEvent);

}

1 个答案:

答案 0 :(得分:1)

在上传的代码中,不会调用OnTimer。为了调用OnTimer,必须首先调用SetTimer。

UINT_PTR SetTimer(
   UINT_PTR nIDEvent,
   UINT nElapse,
   void (CALLBACK* lpfnTimer) ( HWND, UINT, UINT_PTR, DWORD ) 
);

此调用可以在OnInitDialog()中进行:

...
//TODO: Add extra initialization here
//         +---------------- Some unique identifier, to differentiate timers
//         |      +--------- The timeout value,
//         |      |    +---- A callback - NULL in this case because of WM_TIMER
//         |      |    |
//         V      V    V
SetTimer( 9001, 2000, NULL );
...

有关更多详细信息,请参阅MSDN文章:

http://msdn.microsoft.com/en-us/library/49313fdf(VS.80).aspx