examples/Base/Timers/BasicTimer/BasicTimer.cpp

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 how RTimer, the basic timer class, works
00015 //
00016 
00017 #include "CommonFramework.h"
00018 
00019 
00020 void showTimeL(TTime aTime)
00021         {
00022                 // Format time, using system default locale settings
00023                 // and then print the time using system default time
00024                 // separator character (':') in 24 hour clock format.
00025 
00026         TBuf<40> timeString; // Holds the formatted date and time
00027         _LIT(KFormat1,"%:0%H%:1%T%:2%S%:3");
00028         aTime.FormatL(timeString,KFormat1);
00029         _LIT(KFormat2,"(24 hr clock) = %S\n");
00030         console->Printf(KFormat2, &timeString);
00031         }
00032 
00033 
00034 void WaitForKey()
00035         {
00036         _LIT(KTxtPressAnyKey,"Press any key to continue\n\n");
00037         console->Printf(KTxtPressAnyKey);
00038         console->Getch();
00039         }
00040 
00041 
00042 LOCAL_C void doExampleL()
00043     {
00044         RTimer timer;                            // The asynchronous timer and ...
00045         TRequestStatus timerStatus;  // ... its associated request status
00046         timer.CreateLocal();         // Always created for this thread.
00047 
00048                 // do some After() requests
00049         _LIT(KTxt1,"Doing 10 after requests\n");
00050         console->Printf(KTxt1);
00051         for (TInt i=0; i<10; i++)
00052                 {
00053                         // issue and wait for single request
00054                 timer.After(timerStatus,1000000);  // wait 1 second
00055                 User::WaitForRequest(timerStatus); // wait for request to complete
00056                         // display the tick count
00057                 _LIT(KFormat3,"Request count %d\n");
00058                 console->Printf(KFormat3, i);
00059                 }
00060 
00061     WaitForKey(); // wait until a key is pressed
00062 
00063                 // do an At() request
00064         TTime time; // time in microseconds since 0AD nominal Gregorian
00065         _LIT(KTxt2,"The time now is, ");
00066         console->Printf(KTxt2);
00067                 
00068                 // set and print current time
00069         time.HomeTime(); 
00070         showTimeL(time); 
00071                 
00072                 // add 10 seconds to the time
00073         TTimeIntervalSeconds timeIntervalSeconds(10);
00074         time += timeIntervalSeconds;
00075         _LIT(KTxt3,"Doing a request ten seconds from now at, ");
00076         console->Printf(KTxt3);
00077         showTimeL(time); // print the time the request should complete
00078                 
00079                 // issue and wait for single request.
00080                 // set timer to go off in 10 seconds
00081         timer.At(timerStatus,time); 
00082         
00083            // wait for request to complete
00084         User::WaitForRequest(timerStatus); 
00085                 
00086                 // say it's over, and set and print the time again
00087         _LIT(KTxt4,"Your 10 seconds are up\nThe time now is, ");
00088         console->Printf(KTxt4);
00089         
00090            // set time to now
00091         time.HomeTime(); 
00092         
00093            // print the time
00094         showTimeL(time); 
00095                 
00096                 // close timer
00097         timer.Close();
00098         }
00099 

Generated by  doxygen 1.6.2