Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Timers

Timers

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chetan210183
    wrote on last edited by
    #1

    Hi everyone, I am trying to develop a Dialog based application for a device communicating through RS 232 serial Port. The serial port receives the new samples of data every 100 milli seconds. I am trying to display the message in a textbox and write the data to a file. For this purpose, i created a thread for receiving data. I am using a Timer with 100 milli seconds for Updating the textbox and writing the received data to the file. The Problem now i am facing is, when the file gets full, i am trying to display error message and stop the timer. But Timer is not getting stopped by KillTimer().Timer continues in the running state. BOOL MyDlg::OnInitDialog() { .... ... ConfigureSerialPort(); AfxBeginThread(Serial, NULL,THREAD_PRIORITY_NORMAL,0,0,NULL); return TRUE; } void CMyDlg::OnClickStart() { fp = fopen(file_name,"a"); if (NULL == fp) { MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP); KillTimer(m_nTimer); } m_nTimer = SetTimer(1, 100, NULL); fprintf(fp,"%d\n",status); } void CMyDlg::OnTimer(UINT nIDEvent) { OnClickStart(); CDialog::OnTimer(nIDEvent); } UINT Serial(LPVOID pParam) { while(1) { DWORD dwBytesTransferred; ReadFile (hPort, inbuf, 60, &dwBytesTransferred, NULL); Sleep(100); } return TRUE; } Please help me regarding this problem. I am New to MultiThreading Concepts. Thanks. Chetan. Helping others satisfies you...

    N L A 3 Replies Last reply
    0
    • C chetan210183

      Hi everyone, I am trying to develop a Dialog based application for a device communicating through RS 232 serial Port. The serial port receives the new samples of data every 100 milli seconds. I am trying to display the message in a textbox and write the data to a file. For this purpose, i created a thread for receiving data. I am using a Timer with 100 milli seconds for Updating the textbox and writing the received data to the file. The Problem now i am facing is, when the file gets full, i am trying to display error message and stop the timer. But Timer is not getting stopped by KillTimer().Timer continues in the running state. BOOL MyDlg::OnInitDialog() { .... ... ConfigureSerialPort(); AfxBeginThread(Serial, NULL,THREAD_PRIORITY_NORMAL,0,0,NULL); return TRUE; } void CMyDlg::OnClickStart() { fp = fopen(file_name,"a"); if (NULL == fp) { MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP); KillTimer(m_nTimer); } m_nTimer = SetTimer(1, 100, NULL); fprintf(fp,"%d\n",status); } void CMyDlg::OnTimer(UINT nIDEvent) { OnClickStart(); CDialog::OnTimer(nIDEvent); } UINT Serial(LPVOID pParam) { while(1) { DWORD dwBytesTransferred; ReadFile (hPort, inbuf, 60, &dwBytesTransferred, NULL); Sleep(100); } return TRUE; } Please help me regarding this problem. I am New to MultiThreading Concepts. Thanks. Chetan. Helping others satisfies you...

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      chetan210183 wrote:

      The Problem now i am facing is, when the file gets full, i am trying to display error message and stop the timer. But Timer is not getting stopped by KillTimer().Timer continues in the running state.

      This is because you are setting the timer again in OnClickStart. Why are you doing this? You don't have to set it again and again as you are doing now.

      chetan210183 wrote:

      void CMyDlg::OnClickStart() { fp = fopen(file_name,"a"); if (NULL == fp) { MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP); KillTimer(m_nTimer); } m_nTimer = SetTimer(1, 100, NULL); fprintf(fp,"%d\n",status); }

      Look at the lines in bold. You first Kill the timer and then again you set it. So how can you expect it to stop at all.

      chetan210183 wrote:

      void CMyDlg::OnTimer(UINT nIDEvent) { OnClickStart(); CDialog::OnTimer(nIDEvent); }

      Again inside the timer function you are calling OnClickStart which in turn again sets the timer. What you should be doing is...

      void CMyDlg::OnClickStart()
      {
      //set the timer here. Nothing else!
      m_nTimer = SetTimer(1, 100, NULL);
      }

      And now inside the timer function

      void CMyDlg::OnTimer(UINT nIDEvent)
      {
      //write your timer related code here. Not anywhere else!
      fp = fopen(file_name,"a");
      if (NULL == fp)
      {
      MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP);
      KillTimer(m_nTimer);
      }
      }

      Note: Well this is just guess work about what you want to do. You will have to modify the above procedure based on your requirements


      Nibu thomas Software Developer

      C 1 Reply Last reply
      0
      • C chetan210183

        Hi everyone, I am trying to develop a Dialog based application for a device communicating through RS 232 serial Port. The serial port receives the new samples of data every 100 milli seconds. I am trying to display the message in a textbox and write the data to a file. For this purpose, i created a thread for receiving data. I am using a Timer with 100 milli seconds for Updating the textbox and writing the received data to the file. The Problem now i am facing is, when the file gets full, i am trying to display error message and stop the timer. But Timer is not getting stopped by KillTimer().Timer continues in the running state. BOOL MyDlg::OnInitDialog() { .... ... ConfigureSerialPort(); AfxBeginThread(Serial, NULL,THREAD_PRIORITY_NORMAL,0,0,NULL); return TRUE; } void CMyDlg::OnClickStart() { fp = fopen(file_name,"a"); if (NULL == fp) { MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP); KillTimer(m_nTimer); } m_nTimer = SetTimer(1, 100, NULL); fprintf(fp,"%d\n",status); } void CMyDlg::OnTimer(UINT nIDEvent) { OnClickStart(); CDialog::OnTimer(nIDEvent); } UINT Serial(LPVOID pParam) { while(1) { DWORD dwBytesTransferred; ReadFile (hPort, inbuf, 60, &dwBytesTransferred, NULL); Sleep(100); } return TRUE; } Please help me regarding this problem. I am New to MultiThreading Concepts. Thanks. Chetan. Helping others satisfies you...

        L Offline
        L Offline
        Laxman Auti
        wrote on last edited by
        #3

        You wrote:

        void CMyDlg::OnClickStart()
        {
        fp = fopen(file_name,"a");
        if (NULL == fp)
        {
        MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP);
        KillTimer(m_nTimer);
        }
        m_nTimer = SetTimer(1, 100, NULL);
        fprintf(fp,"%d\n",status);
        }
        

        for killing timer use

        KillTimer(1);
        

        i.e what ID you provided to set the timer The Timer returns the timer id when hwnd parameter of the settimer is NULL hope this is enough for you Knock out "T" from CAN'T You 'CAN' if you think you 'CAN' :cool:

        N 1 Reply Last reply
        0
        • L Laxman Auti

          You wrote:

          void CMyDlg::OnClickStart()
          {
          fp = fopen(file_name,"a");
          if (NULL == fp)
          {
          MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP);
          KillTimer(m_nTimer);
          }
          m_nTimer = SetTimer(1, 100, NULL);
          fprintf(fp,"%d\n",status);
          }
          

          for killing timer use

          KillTimer(1);
          

          i.e what ID you provided to set the timer The Timer returns the timer id when hwnd parameter of the settimer is NULL hope this is enough for you Knock out "T" from CAN'T You 'CAN' if you think you 'CAN' :cool:

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          A_Laxman wrote:

          for killing timer use KillTimer(1); i.e what ID you provided to set the timer The Timer returns the timer id when hwnd parameter of the settimer is NULL

          From MSDN: Return value of SetTimer: The timer identifier of the new timer if the function is successful. An application passes this value to the KillTimer member function to kill the timer. Nonzero if successful; otherwise 0.


          Nibu thomas Software Developer

          1 Reply Last reply
          0
          • N Nibu babu thomas

            chetan210183 wrote:

            The Problem now i am facing is, when the file gets full, i am trying to display error message and stop the timer. But Timer is not getting stopped by KillTimer().Timer continues in the running state.

            This is because you are setting the timer again in OnClickStart. Why are you doing this? You don't have to set it again and again as you are doing now.

            chetan210183 wrote:

            void CMyDlg::OnClickStart() { fp = fopen(file_name,"a"); if (NULL == fp) { MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP); KillTimer(m_nTimer); } m_nTimer = SetTimer(1, 100, NULL); fprintf(fp,"%d\n",status); }

            Look at the lines in bold. You first Kill the timer and then again you set it. So how can you expect it to stop at all.

            chetan210183 wrote:

            void CMyDlg::OnTimer(UINT nIDEvent) { OnClickStart(); CDialog::OnTimer(nIDEvent); }

            Again inside the timer function you are calling OnClickStart which in turn again sets the timer. What you should be doing is...

            void CMyDlg::OnClickStart()
            {
            //set the timer here. Nothing else!
            m_nTimer = SetTimer(1, 100, NULL);
            }

            And now inside the timer function

            void CMyDlg::OnTimer(UINT nIDEvent)
            {
            //write your timer related code here. Not anywhere else!
            fp = fopen(file_name,"a");
            if (NULL == fp)
            {
            MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP);
            KillTimer(m_nTimer);
            }
            }

            Note: Well this is just guess work about what you want to do. You will have to modify the above procedure based on your requirements


            Nibu thomas Software Developer

            C Offline
            C Offline
            chetan210183
            wrote on last edited by
            #5

            Thanks for the Reply. But without a messagebox, the code excecutes properly. When messageBox is inserted it is leading to infinite loop. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/timers/timerreference/timermessages/wm\_timer.asp In the above link, they have mentioned a statement like below. "The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue. " So instead of Timers , can i use threads and loop the function? I mean replacing the Timers with Threads. Thanks. Chetan. Helping others satisfies you...

            N 1 Reply Last reply
            0
            • C chetan210183

              Thanks for the Reply. But without a messagebox, the code excecutes properly. When messageBox is inserted it is leading to infinite loop. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/timers/timerreference/timermessages/wm\_timer.asp In the above link, they have mentioned a statement like below. "The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue. " So instead of Timers , can i use threads and loop the function? I mean replacing the Timers with Threads. Thanks. Chetan. Helping others satisfies you...

              N Offline
              N Offline
              Nibu babu thomas
              wrote on last edited by
              #6

              chetan210183 wrote:

              "The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue. "

              Yeah.

              chetan210183 wrote:

              So instead of Timers , can i use threads and loop the function? I mean replacing the Timers with Threads.

              Why not!


              Nibu thomas Software Developer

              C 1 Reply Last reply
              0
              • N Nibu babu thomas

                chetan210183 wrote:

                "The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue. "

                Yeah.

                chetan210183 wrote:

                So instead of Timers , can i use threads and loop the function? I mean replacing the Timers with Threads.

                Why not!


                Nibu thomas Software Developer

                C Offline
                C Offline
                chetan210183
                wrote on last edited by
                #7

                Thanks a lot. Can u please suggest links where i can get sample programs on Threads replacing the Timers. Please...... Chetan. Helping others satisfies you...

                N 1 Reply Last reply
                0
                • C chetan210183

                  Thanks a lot. Can u please suggest links where i can get sample programs on Threads replacing the Timers. Please...... Chetan. Helping others satisfies you...

                  N Offline
                  N Offline
                  Nibu babu thomas
                  wrote on last edited by
                  #8

                  chetan210183 wrote:

                  Can u please suggest links where i can get sample programs on Threads replacing the Timers. Please......

                  I don't know if this is what you are searching for. Here[^] is it. Well it has got some topics on which multithreading depends. Will make a good reading for you.


                  Nibu thomas Software Developer

                  1 Reply Last reply
                  0
                  • C chetan210183

                    Hi everyone, I am trying to develop a Dialog based application for a device communicating through RS 232 serial Port. The serial port receives the new samples of data every 100 milli seconds. I am trying to display the message in a textbox and write the data to a file. For this purpose, i created a thread for receiving data. I am using a Timer with 100 milli seconds for Updating the textbox and writing the received data to the file. The Problem now i am facing is, when the file gets full, i am trying to display error message and stop the timer. But Timer is not getting stopped by KillTimer().Timer continues in the running state. BOOL MyDlg::OnInitDialog() { .... ... ConfigureSerialPort(); AfxBeginThread(Serial, NULL,THREAD_PRIORITY_NORMAL,0,0,NULL); return TRUE; } void CMyDlg::OnClickStart() { fp = fopen(file_name,"a"); if (NULL == fp) { MessageBox ("Error Writing File",NULL, MB_OK|MB_ICONSTOP); KillTimer(m_nTimer); } m_nTimer = SetTimer(1, 100, NULL); fprintf(fp,"%d\n",status); } void CMyDlg::OnTimer(UINT nIDEvent) { OnClickStart(); CDialog::OnTimer(nIDEvent); } UINT Serial(LPVOID pParam) { while(1) { DWORD dwBytesTransferred; ReadFile (hPort, inbuf, 60, &dwBytesTransferred, NULL); Sleep(100); } return TRUE; } Please help me regarding this problem. I am New to MultiThreading Concepts. Thanks. Chetan. Helping others satisfies you...

                    A Offline
                    A Offline
                    Anilkumar K V
                    wrote on last edited by
                    #9

                    You can mask the COM port, and wait for events happen in COM port and then only update the text box.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups