Symbian3/SDK/Source/GUID-E5B5C3CA-B256-5B29-AB90-590676444C85.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Tue, 20 Jul 2010 12:00:49 +0100
changeset 13 48780e181b38
parent 0 89d6a7a84779
permissions -rw-r--r--
Week 28 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 1897 and Bug 1522.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-E5B5C3CA-B256-5B29-AB90-590676444C85" xml:lang="en"><title>How
to start a periodic timer</title><shortdesc>Provides code snippets to show you how to start a periodic timer</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>A periodic timer, <codeph>CPeriodic</codeph>, invokes a function at regular
intervals. You must wrap the function to be called in a <codeph>TCallBack</codeph>. </p>
<p>First, we provide a callback function for the timer to call. The callback
function can be a static member, i.e. <codeph>TInt X::Foo(TAny *)</codeph> or
global, i.e. <codeph>TInt Foo(TAny *)</codeph>. The <codeph>TAny*</codeph> can
point to any object that we specify when we start the timer.</p>
<p>It is awkward to deal with <codeph>TAny*</codeph>, and more convenient
to write handler code from a non-static member function, so here we code a
static function to invoke a non-static function <codeph>DoTick()</codeph> as
follows:</p>
<codeblock id="GUID-8D673BE4-1F28-58E0-868D-98B48DD91C60" xml:space="preserve">TInt CPeriodicRunner::Tick(TAny* aObject)
    {
    // cast, and call non-static function
    ((CPeriodicRunner*)aObject)-&gt;DoTick();
    return 1;
    }
</codeblock>
<p>The next piece of code creates a <codeph>CPeriodic</codeph> and sets it
off with one-second ticks. The callback is specified to call the static <codeph>Tick()</codeph> function,
passing the <codeph>this</codeph> pointer (which we use in <codeph>Tick()</codeph> to
call <codeph>DoTick()</codeph>).</p>
<codeblock id="GUID-092F13DD-9158-5245-A1FE-101EFB305D15" xml:space="preserve">void CPeriodicRunner::StartTimer()
    {
    const TInt tickInterval=1000000;
    iPeriodic=CPeriodic::NewL(0); // neutral priority
    iPeriodic-&gt;StartInMicroSeconds(tickInterval,tickInterval,TCallBack(Tick, this));
    }
</codeblock>
</conbody></concept>