Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

CTIME and CTimeSpan in MFC

Posted on 2005-04-15
4
Medium Priority
?
1,015 Views
Last Modified: 2013-12-14
How can I use the CTime classes,to repeat an actions say printf for example every 10 seconds.
It seemed easy,but somehow I cannot get it right.Please help as soon as possible.
Thank you
0
Comment
Question by:samroyc
  • 2
4 Comments
 
LVL 1

Expert Comment

by:dumbo2569
ID: 13791228
Why don't you just use the Sleep function?

#include <afx.h>

int main()
{
    Sleep(10000);    // sleeps for 10,000 miliseconds
    return 0;
}

0
 
LVL 14

Expert Comment

by:wayside
ID: 13794689
You could also create a timer that fires every 10 seconds and have it call a timer function to do your printf-ing.

The problem with sleep is that unless you put the sleep in a separate thread, your application will stop responding to any user input.
0
 
LVL 9

Expert Comment

by:rcarlan
ID: 13795409
The example code below is based on a command line (console) application with MFC support :


#include "stdafx.h"
#include <iostream>
#include <string>


CWinApp theApp;

using namespace std;


bool g_bStop = false;
const unsigned g_nSecondsBetweenTicks = 10;


UINT HeartBeat(LPVOID nRate)
{
      while (!g_bStop)
      {
            Sleep((DWORD)nRate);
            cout << "Tick" << endl;
      }

      return 0;
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
      int nRetCode = 0;

      // initialize MFC and print and error on failure
      if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
      {
            // TODO: change error code to suit your needs
            cerr << _T("Fatal Error: MFC initialization failed") << endl;
            nRetCode = 1;
      }
      else
      {
            AfxBeginThread(HeartBeat, (LPVOID)g_nSecondsBetweenTicks*1000);

            string sInput;
            do
            {
                  cin >> sInput;

            } while(stricmp(sInput.c_str(), "stop"));
      }

      return nRetCode;
}

Radu
0
 
LVL 9

Accepted Solution

by:
rcarlan earned 1000 total points
ID: 13795447
Sorry, I got ahead of myself :-)

See revised code (this time with comments):


#include "stdafx.h"
#include <iostream>
#include <string>


CWinApp theApp;

using namespace std;


bool g_bStop = false;
      // we'll use this to signal the heart beat to stop

const unsigned g_nSecondsBetweenTicks = 10;
      // seconds between heart beats



UINT HeartBeat(LPVOID nRate)
{
      while (!g_bStop)
      {
            cout << "Tick" << endl;
            Sleep((DWORD)nRate);
      }

      return 0;
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
      int nRetCode = 0;

      // initialize MFC and print and error on failure
      if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
      {
            // TODO: change error code to suit your needs
            cerr << _T("Fatal Error: MFC initialization failed") << endl;
            nRetCode = 1;
      }
      else
      {
            // start the heart beat thread
            CWinThread* pHeartBeat = AfxBeginThread(HeartBeat, (LPVOID)(g_nSecondsBetweenTicks*1000));

            // do your main stuff
            string sInput;
            do
            {
                  cin >> sInput;

                  // process input (you may want to serialize input and output calls with a critical section)

            } while(stricmp(sInput.c_str(), "stop"));

            // signal the hear beat to stop
            g_bStop = true;

            // wait for heart beat to stop
            WaitForSingleObject(pHeartBeat->m_hThread, INFINITE);

            // exit
      }

      return nRetCode;
}


Radu
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question