00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // Show asynchronous programming (without active objects) 00015 // Example shows how a wait loop can be used to identify how a 00016 // request completed and service its completion 00017 // 00018 00019 00020 #include "CommonFramework.h" 00021 #include <e32math.h> 00022 00023 // 00024 // utility functions 00025 // 00026 00027 LOCAL_D TInt64 smallRandSeed; 00028 00029 LOCAL_C TInt smallRand() 00030 { 00031 // produce small random numbers in range 0..9 00032 TInt bigResult=Math::Rand(smallRandSeed);// result uses full 32-bit range 00033 return bigResult % 10; // return result mod 10 00034 } 00035 00036 LOCAL_C void sleep(TInt aTenths) 00037 { 00038 // sleep for an interval measured in tenths of a second 00039 User::After(aTenths*100000); // just let the User function do it for us 00040 } 00041 00042 00043 // Do the example 00044 LOCAL_C void doExampleL() 00045 { 00046 // create and initialize heartbeat timer 00047 RTimer heartbeat; // heartbeat timer 00048 TRequestStatus heartbeatStatus; // request status associated with it 00049 heartbeat.CreateLocal(); // always created for this thread 00050 00051 // issue first heartbeat request 00052 heartbeat.After(heartbeatStatus,1000000); // request completion 00053 // after 1 second 00054 TInt heartbeatTick=0; // counts heartbeat ticks 00055 00056 // wait loop 00057 for (;;) 00058 { 00059 // wait for any request 00060 User::WaitForAnyRequest(); 00061 // find out which request completed, and handle it 00062 if (heartbeatStatus!=KRequestPending) 00063 { 00064 // heartbeat completed so service request 00065 _LIT(KMsgServicing,"Servicing heartbeat tick %d ...\n"); 00066 console->Printf(KMsgServicing,heartbeatTick); 00067 // take some time over it 00068 sleep(smallRand()); 00069 _LIT(KMsgServiced,"... heartbeat tick %d serviced\n"); 00070 console->Printf(KMsgServiced,heartbeatTick); 00071 // test whether processing should finish 00072 if (heartbeatTick >= 10) 00073 { 00074 // 10 heart-beats: processing finished 00075 _LIT(KMsgFinishing,"Finishing\n"); 00076 console->Printf(KMsgFinishing); 00077 // finish wait loop 00078 break; 00079 } 00080 // re-issue request 00081 heartbeatTick++; // increment tick 00082 // counter 00083 heartbeat.After(heartbeatStatus,1000000); // request completion 00084 // after another second 00085 } 00086 else 00087 { 00088 // stray signal 00089 _LIT(KMsgStraySignal,"Stray signal\n"); 00090 User::Panic(KMsgStraySignal, 1); // panic! 00091 } 00092 } 00093 00094 // close timer 00095 heartbeat.Close(); // close timer 00096 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.