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. timer start problem

timer start problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
7 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.
  • S Offline
    S Offline
    susanne1
    wrote on last edited by
    #1

    Hallo, I need to start a timer like the following to measure the time that my appliction requires to do someting: void CMiniClass :: DoSomething() { theApp.m_pMainWnd ->SetTimer (MY_TIMER,1000,NULL); while (i > 0) { DoStuff(); } //theApp.m_pMainWnd ->KillTimer (MY_TIMER); } in the CMainFrame.cpp i overrode the OnTimer() from the baseclass: void CMainFrame::OnTimer(UINT nIDEvent) { m_bFlagStartTimer = true; CFrameWnd::OnTimer(nIDEvent); CString strTimer(_T("")); m_nSeconds++; if(m_nSeconds == 60) { m_nMinutes++; m_nSeconds = 0; } if(m_nMinutes == 60) { m_nHours++; m_nMinutes = 0; } if(m_nHours == 24) { m_nHours = 0; } strTimer.Format("%d:%d:%d", m_nHours, m_nMinutes, m_nSeconds); MessageBox(strTimer,0,0); } the problem is the application starts the Timer(in the Messagebox) after he finished the while-loop,so i can not know how long my appliction was BUSY with this loop, and if i activate theApp.m_pMainWnd ->KillTimer (MY_TIMER); the Timer would be KILLED and then the while-llop starts, i need to do this without using Thread. Please help to fix this problem. Thanks.

    _ S 2 Replies Last reply
    0
    • S susanne1

      Hallo, I need to start a timer like the following to measure the time that my appliction requires to do someting: void CMiniClass :: DoSomething() { theApp.m_pMainWnd ->SetTimer (MY_TIMER,1000,NULL); while (i > 0) { DoStuff(); } //theApp.m_pMainWnd ->KillTimer (MY_TIMER); } in the CMainFrame.cpp i overrode the OnTimer() from the baseclass: void CMainFrame::OnTimer(UINT nIDEvent) { m_bFlagStartTimer = true; CFrameWnd::OnTimer(nIDEvent); CString strTimer(_T("")); m_nSeconds++; if(m_nSeconds == 60) { m_nMinutes++; m_nSeconds = 0; } if(m_nMinutes == 60) { m_nHours++; m_nMinutes = 0; } if(m_nHours == 24) { m_nHours = 0; } strTimer.Format("%d:%d:%d", m_nHours, m_nMinutes, m_nSeconds); MessageBox(strTimer,0,0); } the problem is the application starts the Timer(in the Messagebox) after he finished the while-loop,so i can not know how long my appliction was BUSY with this loop, and if i activate theApp.m_pMainWnd ->KillTimer (MY_TIMER); the Timer would be KILLED and then the while-llop starts, i need to do this without using Thread. Please help to fix this problem. Thanks.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      You shouldn't be using a timer here. You should ideally get the time first, start your job, get the time again after you're job completes and then compare the first and second time.

      «_Superman_» I love work. It gives me something to do between weekends.

      S 1 Reply Last reply
      0
      • S susanne1

        Hallo, I need to start a timer like the following to measure the time that my appliction requires to do someting: void CMiniClass :: DoSomething() { theApp.m_pMainWnd ->SetTimer (MY_TIMER,1000,NULL); while (i > 0) { DoStuff(); } //theApp.m_pMainWnd ->KillTimer (MY_TIMER); } in the CMainFrame.cpp i overrode the OnTimer() from the baseclass: void CMainFrame::OnTimer(UINT nIDEvent) { m_bFlagStartTimer = true; CFrameWnd::OnTimer(nIDEvent); CString strTimer(_T("")); m_nSeconds++; if(m_nSeconds == 60) { m_nMinutes++; m_nSeconds = 0; } if(m_nMinutes == 60) { m_nHours++; m_nMinutes = 0; } if(m_nHours == 24) { m_nHours = 0; } strTimer.Format("%d:%d:%d", m_nHours, m_nMinutes, m_nSeconds); MessageBox(strTimer,0,0); } the problem is the application starts the Timer(in the Messagebox) after he finished the while-loop,so i can not know how long my appliction was BUSY with this loop, and if i activate theApp.m_pMainWnd ->KillTimer (MY_TIMER); the Timer would be KILLED and then the while-llop starts, i need to do this without using Thread. Please help to fix this problem. Thanks.

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        susanne1 wrote:

        I need to start a timer like the following to measure the time that my appliction requires to do someting

        You're using the wrong sort of timer - the windows timer sends one of your windows a WM_TIMER message when the time you wanted has expired. You imply you want to measure how long something took to run. They way to do that is to find the time when the activity starts, the time when it ends and determine the difference between them. I generally use the high-resolution counter[^] for that.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        S 1 Reply Last reply
        0
        • _ _Superman_

          You shouldn't be using a timer here. You should ideally get the time first, start your job, get the time again after you're job completes and then compare the first and second time.

          «_Superman_» I love work. It gives me something to do between weekends.

          S Offline
          S Offline
          susanne1
          wrote on last edited by
          #4

          i used this code but the problem it starts first when the appliction has finished the job. CFrameWnd::OnTimer(nIDEvent); CTime timer; CString strTimer (_T("")); timer = CTime::GetCurrentTime (); if(((m_nSeconds = timer.GetSecond ()) == 0) && (m_bFlagTimer == true)) { m_nMinutes++; } if(((timer.GetMinute ()) == 0) && (m_bFlagTimer == true)) { m_nHours++; } wsprintf((LPTSTR)strTimer.GetString (),"%02i:%02i:%02i",m_nHours, m_nMinutes, m_nSeconds); m_wndStatusBar.SetPaneText(5,LPCSTR(strTimer.GetString ()),true i need to count parallel to the task when it runs.

          1 Reply Last reply
          0
          • S Stuart Dootson

            susanne1 wrote:

            I need to start a timer like the following to measure the time that my appliction requires to do someting

            You're using the wrong sort of timer - the windows timer sends one of your windows a WM_TIMER message when the time you wanted has expired. You imply you want to measure how long something took to run. They way to do that is to find the time when the activity starts, the time when it ends and determine the difference between them. I generally use the high-resolution counter[^] for that.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            S Offline
            S Offline
            susanne1
            wrote on last edited by
            #5

            i used this code but the problem it starts first when the appliction has finished the job. CFrameWnd::OnTimer(nIDEvent); CTime timer; CString strTimer (_T("")); timer = CTime::GetCurrentTime (); if(((m_nSeconds = timer.GetSecond ()) == 0) && (m_bFlagTimer == true)) { m_nMinutes++; } if(((timer.GetMinute ()) == 0) && (m_bFlagTimer == true)) { m_nHours++; } wsprintf((LPTSTR)strTimer.GetString (),"%02i:%02i:%02i",m_nHours, m_nMinutes, m_nSeconds); m_wndStatusBar.SetPaneText(5,LPCSTR(strTimer.GetString ()),true i need to count parallel to the task when it runs.

            C S 2 Replies Last reply
            0
            • S susanne1

              i used this code but the problem it starts first when the appliction has finished the job. CFrameWnd::OnTimer(nIDEvent); CTime timer; CString strTimer (_T("")); timer = CTime::GetCurrentTime (); if(((m_nSeconds = timer.GetSecond ()) == 0) && (m_bFlagTimer == true)) { m_nMinutes++; } if(((timer.GetMinute ()) == 0) && (m_bFlagTimer == true)) { m_nHours++; } wsprintf((LPTSTR)strTimer.GetString (),"%02i:%02i:%02i",m_nHours, m_nMinutes, m_nSeconds); m_wndStatusBar.SetPaneText(5,LPCSTR(strTimer.GetString ()),true i need to count parallel to the task when it runs.

              C Offline
              C Offline
              Chandrasekharan P
              wrote on last edited by
              #6

              There is no point reposting the same code. there was an answer given by somebody. try that out. if you get stuck as again. you will get assistance

              1 Reply Last reply
              0
              • S susanne1

                i used this code but the problem it starts first when the appliction has finished the job. CFrameWnd::OnTimer(nIDEvent); CTime timer; CString strTimer (_T("")); timer = CTime::GetCurrentTime (); if(((m_nSeconds = timer.GetSecond ()) == 0) && (m_bFlagTimer == true)) { m_nMinutes++; } if(((timer.GetMinute ()) == 0) && (m_bFlagTimer == true)) { m_nHours++; } wsprintf((LPTSTR)strTimer.GetString (),"%02i:%02i:%02i",m_nHours, m_nMinutes, m_nSeconds); m_wndStatusBar.SetPaneText(5,LPCSTR(strTimer.GetString ()),true i need to count parallel to the task when it runs.

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                What you want to do is something like this (using code from you original post):

                void CMiniClass :: DoSomething()
                {
                LARGE_INTEGER startTime, endTime;
                QueryPerformanceCounter(&startTime);
                while (i > 0)
                {
                DoStuff();
                }
                QueryPerformanceCounter(&endTime);
                LARGE_INTEGER elapsedTime;
                elapsedTime.QuadPart = endTime.QuadPart - startTime.QuadPart;
                LARGE_INTEGER timerFreq;
                QueryPerformanceFrequency(&timerFreq);
                const double elapsedTimeInSeconds = (double)elapsedTime.QuadPart / (double)timerFreq.QuadPart;
                }

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                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