examples/Base/Timers/BasicTimer/BasicTimer.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: Show how RTimer, the basic timer class, works  
00028 */
00029 
00030 
00031 #include "CommonFramework.h"
00032 
00033 
00034 void showTimeL(TTime aTime)
00035         {
00036                 // Format time, using system default locale settings
00037                 // and then print the time using system default time
00038                 // separator character (':') in 24 hour clock format.
00039 
00040         TBuf<40> timeString; // Holds the formatted date and time
00041         _LIT(KFormat1,"%:0%H%:1%T%:2%S%:3");
00042         aTime.FormatL(timeString,KFormat1);
00043         _LIT(KFormat2,"(24 hr clock) = %S\n");
00044         console->Printf(KFormat2, &timeString);
00045         }
00046 
00047 
00048 void WaitForKey()
00049         {
00050         _LIT(KTxtPressAnyKey,"Press any key to continue\n\n");
00051         console->Printf(KTxtPressAnyKey);
00052         console->Getch();
00053         }
00054 
00055 
00056 LOCAL_C void doExampleL()
00057     {
00058         RTimer timer;                            // The asynchronous timer and ...
00059         TRequestStatus timerStatus;  // ... its associated request status
00060         timer.CreateLocal();         // Always created for this thread.
00061 
00062                 // do some After() requests
00063         _LIT(KTxt1,"Doing 10 after requests\n");
00064         console->Printf(KTxt1);
00065         for (TInt i=0; i<10; i++)
00066                 {
00067                         // issue and wait for single request
00068                 timer.After(timerStatus,1000000);  // wait 1 second
00069                 User::WaitForRequest(timerStatus); // wait for request to complete
00070                         // display the tick count
00071                 _LIT(KFormat3,"Request count %d\n");
00072                 console->Printf(KFormat3, i);
00073                 }
00074 
00075     WaitForKey(); // wait until a key is pressed
00076 
00077                 // do an At() request
00078         TTime time; // time in microseconds since 0AD nominal Gregorian
00079         _LIT(KTxt2,"The time now is, ");
00080         console->Printf(KTxt2);
00081                 
00082                 // set and print current time
00083         time.HomeTime(); 
00084         showTimeL(time); 
00085                 
00086                 // add 10 seconds to the time
00087         TTimeIntervalSeconds timeIntervalSeconds(10);
00088         time += timeIntervalSeconds;
00089         _LIT(KTxt3,"Doing a request ten seconds from now at, ");
00090         console->Printf(KTxt3);
00091         showTimeL(time); // print the time the request should complete
00092                 
00093                 // issue and wait for single request.
00094                 // set timer to go off in 10 seconds
00095         timer.At(timerStatus,time); 
00096         
00097            // wait for request to complete
00098         User::WaitForRequest(timerStatus); 
00099                 
00100                 // say it's over, and set and print the time again
00101         _LIT(KTxt4,"Your 10 seconds are up\nThe time now is, ");
00102         console->Printf(KTxt4);
00103         
00104            // set time to now
00105         time.HomeTime(); 
00106         
00107            // print the time
00108         showTimeL(time); 
00109                 
00110                 // close timer
00111         timer.Close();
00112         }
00113 

Generated by  doxygen 1.6.2