1
votes

I have been looking at this code and I am having trouble understanding the functionality of the OnTimer function. I am quite new to VC++ so all of this is new knowledge for me.

Can any one tell me when OnTimer event will be fired ? I have pasted my code below for review.

(new to VC++) (sorry for bad formatting)

iSCSIDlg.h : header file

#define WM_RECVDATA     WM_USER+1

class CISCSIDlg : public CDialog
{
public:
    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);

protected:
    afx_msg void OnTimer(UINT nIDEvent);
    DECLARE_MESSAGE_MAP()

};

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();
    enum { IDD = IDD_ABOUTBOX };
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);

protected:
    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);
    return TRUE;
}

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

void CISCSIDlg::OnPaint() 
{

}

HCURSOR CISCSIDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

DWORD WINAPI CISCSIDlg::RecvProc(LPVOID lpParameter)
{
    SOCKET sock=((RECVPARAM*)lpParameter)->sock;
    HWND hwnd=((RECVPARAM*)lpParameter)->hwnd;
    delete 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

1 Answers

1
votes

In the Code uploaded, OnTimer will not be called. In order for OnTimer to be called, an initial call to SetTimer must be made.

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

This call can be made in 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 );
...

See the MSDN article for more detail:

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