VistaTickerTest.cpp 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include <VistaInterProcComm/Concurrency/VistaTicker.h>
#include <VistaBase/VistaTimeUtils.h>

#include <iostream>

using namespace std;

class TickerCallback : public VistaTicker::AfterPulseFunctor
{
public:
	inline bool operator()()
	{
13
		cout << "Ticker callback called, text is: " << sText << endl;
14 15 16 17
		return true;
	};

	int nGranularityMS;
18
	std::string sText;
19 20 21 22 23 24 25
};

int main( int, char** )
{
	VistaTicker oTicker;
	
	TickerCallback oCallback;
26
	oCallback.sText = "First call";
27 28 29 30 31 32 33 34
	oCallback.nGranularityMS = oTicker.GetGranularity();

	VistaTicker::TriggerContext oTickerContext( oTicker.GetGranularity(), true );
	oTicker.SetAfterPulseFunctor( &oCallback ); 
	oTicker.AddTrigger( &oTickerContext );

	oTicker.StartTicker();
	
35
	VistaTimeUtils::Sleep( int( 2*1e3 ) );
36 37 38 39

	oTicker.StopTicker();
	oTicker.RemTrigger( &oTickerContext );
	oTicker.SetAfterPulseFunctor( NULL ); // Otherwise Ticker will delete callback instance
40 41 42 43 44 45 46 47 48 49 50 51


	oCallback.sText = "Second call";
	VistaTicker* pTicker = new VistaTicker( 100 );
	pTicker->AddTrigger( new VistaTicker::TriggerContext( 100, true ) );
	pTicker->SetAfterPulseFunctor( &oCallback );
	pTicker->StartTicker();
	VistaTimeUtils::Sleep( int( 2 * 1e3 ) );
	pTicker->StopTicker();
	pTicker->SetAfterPulseFunctor( NULL ); // Otherwise Ticker will delete callback instance
	delete pTicker;

52 53
	return 0;
}