|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE concept |
|
11 PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd"> |
|
12 <concept id="GUID-E5B5C3CA-B256-5B29-AB90-590676444C85" xml:lang="en"><title>How |
|
13 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> |
|
14 <p>A periodic timer, <codeph>CPeriodic</codeph>, invokes a function at regular |
|
15 intervals. You must wrap the function to be called in a <codeph>TCallBack</codeph>. </p> |
|
16 <p>First, we provide a callback function for the timer to call. The callback |
|
17 function can be a static member, i.e. <codeph>TInt X::Foo(TAny *)</codeph> or |
|
18 global, i.e. <codeph>TInt Foo(TAny *)</codeph>. The <codeph>TAny*</codeph> can |
|
19 point to any object that we specify when we start the timer.</p> |
|
20 <p>It is awkward to deal with <codeph>TAny*</codeph>, and more convenient |
|
21 to write handler code from a non-static member function, so here we code a |
|
22 static function to invoke a non-static function <codeph>DoTick()</codeph> as |
|
23 follows:</p> |
|
24 <codeblock id="GUID-8D673BE4-1F28-58E0-868D-98B48DD91C60" xml:space="preserve">TInt CPeriodicRunner::Tick(TAny* aObject) |
|
25 { |
|
26 // cast, and call non-static function |
|
27 ((CPeriodicRunner*)aObject)->DoTick(); |
|
28 return 1; |
|
29 } |
|
30 </codeblock> |
|
31 <p>The next piece of code creates a <codeph>CPeriodic</codeph> and sets it |
|
32 off with one-second ticks. The callback is specified to call the static <codeph>Tick()</codeph> function, |
|
33 passing the <codeph>this</codeph> pointer (which we use in <codeph>Tick()</codeph> to |
|
34 call <codeph>DoTick()</codeph>).</p> |
|
35 <codeblock id="GUID-092F13DD-9158-5245-A1FE-101EFB305D15" xml:space="preserve">void CPeriodicRunner::StartTimer() |
|
36 { |
|
37 const TInt tickInterval=1000000; |
|
38 iPeriodic=CPeriodic::NewL(0); // neutral priority |
|
39 iPeriodic->StartInMicroSeconds(tickInterval,tickInterval,TCallBack(Tick, this)); |
|
40 } |
|
41 </codeblock> |
|
42 </conbody></concept> |