Solved

C# Timer class equivalent in C++

Posted on 2013-05-20
8
1,091 Views
Last Modified: 2013-06-05
Hey Experts,

Is there a C++ (Using Visual C++) equivalent for the Timer class in C#?

I need to run code in a separate thread after a certain amount of time.

Thanks for any help!

-Jeff
0
Comment
Question by:jeffiepoo
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39182772
Yes there is a native version of timers (that's what C# is using in the background), see http://msdn.microsoft.com/en-us/library/windows/desktop/ms632592%28v=vs.85%29.aspx ("Timers"), you will find code samples at http://msdn.microsoft.com/en-us/library/windows/desktop/ms644901%28v=vs.85%29.aspx ("Using Timers"). These are the 'simpler' version of timers that use either callback functions or Windows messages, then there is also the more advaced concept of Waitable Timer Objects (http://msdn.microsoft.com/en-us/library/windows/desktop/ms687012%28v=vs.85%29.aspx). For these, you will find examples at http://www.codeproject.com/Articles/1236/Timers-Tutorial ("Timers Tutorial"). This article also covers your third option, the Multimedia Timers.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39182799
Oh, and here's a class wrapping the more complex timers up: http://www.codeproject.com/Articles/146617/Simple-C-Timer-Wrapper ("Simple C++ Timer Wrapper")

BTW, if you are using C++.NET, you might already be comfortable with this http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=VS.71%29.aspx ("Timer Class")
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39186972
the simplest method is to call
Sleep(milliseconds);

Open in new window


in your thread.

if you need an absolute timer you would do like

SYSTEMTIME st = { 2013, 3, 5, 22, 13, 0, 0, 0 };
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
HANDLE h = CreateWaitableTimer(NULL, FALSE);
SetWaitableTimer(h, (LARGE_INTEGER*)&ft,  0, NULL, NULL, FALSE);
WaitForSingleObject(h, INFINITE);

Open in new window


of course you need to add error handling to the above code.

Sara
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 6

Author Comment

by:jeffiepoo
ID: 39195021
I've been looking at SetTimer vs SetWaitableTimer. SetTimer seems to be a little simpler so I might use that.

Will the callback method be run on another thread? Any idea how this works?

What if I want the callback to modify data within the class it is in? Is that a problem?

Also, I can't block on the timer, I need to set it and forget it.

If not, I think this is my answer.

Another person recommended a TimerQueue which seems very hairy.

-Jeff
0
 
LVL 86

Expert Comment

by:jkr
ID: 39195114
MSDN is unspecific about the thread context of the callback function used there, but since it is called asynchronously, you can assume it is a separate thread. However, there are two caveats:

- it is a 'plain' function or a static member, not a class' member.
- you can't pass any individual data, so it is hard to manipulate any class members

Furthermore, 'SetTimer()' requires applications that use it to run a window with a message queue, if you don't have one, this won't work. Adifferent approach would be creating a thread on your own that uses 'Sleep()', e.g.

class TheClass { // your class
public:

  void DoSomething();
};

struct ThreadArgs {

  TheClass* pClass;
  DWORD dwInterval;
};

DWORD WINAPI TimerThread ( LPVOID pv) {

  ThreadArgs* p = (ThreadArgs*) pv;

  for (;;) { // does not have to be a loop, just to demonstrate how to do that periodically

    Sleep(p->dwInterval);

    p->pClass->DoSomething();

  }

   return 0; // makes the compiler happy
};

//...

TheClass c;

ThreadArgs ta;

ta.pClass = &c;
ta.dwInterval = 5000; // 5s

DWORD dwTID;

HANDLE hThread = CreateThread(NULL, 0, TimerThread, (LPVOID) &ta, 0, &dwTID);

Open in new window

0
 
LVL 33

Expert Comment

by:sarabande
ID: 39197539
as told by jkr, SetTimer will invoke a message loop timer which will call the callback function from current message pump. therefore it is not running asynchronously in a thread but synchronously in the main (ui) thread. because of this you may not do any lengthy operation in the callback or even sleep cause that would freeze the screen and make the program non-responsive. but, of course you could communicate from OnTimer callback with a thread started prior to calling SetTimer. if doing so  the SetTimer/OnTimer is not simpler than SetWaitableTimer cause if it would require an additional synchronization with a thread. the SetTimer/OnTimer is good for periodical updating of controls, for example a progress counter. you also could start a thread from callback which did not require further synchronization bat calls back for example by posting messages to one of the active windows.

Sara
0
 
LVL 6

Accepted Solution

by:
jeffiepoo earned 0 total points
ID: 39212091
I ended up using CreateTimerQueueTimer, I had to pass in a pointer to the current object using 'this' so I could reinterpret the cast to the object type and call a private method that had access to member variables. I had to do this because the callback method is static.

Thanks for the help guys! Although I went with another approach.

-Jeff
0
 
LVL 6

Author Closing Comment

by:jeffiepoo
ID: 39221523
CreateTimerQueueTimer does everything I need, and in the manner I needed it. The other timers were complex as well, but didn't functionally work as well.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the reā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 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