Simple delays and local time.
Len Heins -- lenheins@iafrica.com Monday, January 08, 1996 Salutations All I wish to put a delay of 100ms in my code - are there any Windows 3.1 or MFC functions that will allow me to do this (like the Turbo C delay() and sleep() functions)? I know this is not a great idea in a cooperative multitasking environment, but using timers will simply complicate the issue I think. Also, I cannot seem to get the correct time from Windows. I have tried using the CTime MFC class and the op. systems time() function, but I cannot get the time as I have set in my CMOS setup. Can someone please explain the error of my ways! Thanks to all who reply Len Len J Heins lenheins@iafrica.com South Africa
Brad Wilson -- bradw@netnet.net Monday, January 08, 1996 >> I wish to put a delay of 100ms in my code - are there any Windows 3.1 or MFC >> functions that will allow me to do this (like the Turbo C delay() and >> sleep() functions)? No. You can cobble one together for yourself, though, like this one: DWORD msEnd = timeGetTime( ) + 100; // 100ms while( msEnd < timeGetTime( )) { // Do nothing! } (NOTE: timeGetTime() is a multimedia API function, are requires that you include MMSYSTEM.H and link with MMSYSTEM.LIB) That will completely lock up Windows 3.1 for 100 milliseconds. If you want to sleep for 100ms but let the system live during that time (HIGHLY recommended), you can do this: DWORD msEnd = timeGetTime( ) + 100; // 100ms MSG msg; while( msEnd < timeGetTime( )) { // Dispatch messages while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } That's a very useful function to have (all caveats regarding PeekMessage() loops apply, so don't make your delays too long). You'll have to do some extra work if your application cannot handle re-entry because of message delivery, as well as if the main window that is displayed is a dialog box (ie, call IsDialogMessage() too). Good luck! Brad -- class CBradWilson : public CWorldWatchProgrammingTeam { public: CString GetInetAddr() { return CString("bradw@exptech.com"); } CString GetPhone() { return CString("+1 (810) 620-9803"); } CString GetURL() { return CString("http://www.exptech.com"); } CString GetDisclaimer() { return CString("All I say is fact :-p"); } }; "When you look into these eyes, do you see civilized, or Son of Sam? Will you love or do you hate? Why do you hesitate?"
Mario Contestabile -- Mario_Contestabile.UOS__MTL@UOSMTL2.universal.com Tuesday, January 09, 1996 CTime t = CTime::GetCurrentTime(); or struct tm when; time_t now; time(&now); when = *localtime(&now); CTime theTime = now; >I wish to put a delay of 100ms in my code - are there any Windows 3.1 or MFC >functions that will allow me to do this (like the Turbo C delay() and >sleep() functions)? I know this is not a great idea in a cooperative >multitasking environment, but using timers will simply complicate the issue >I think. mcontest@universal.com
Christophe.Nasarre@col.bsf.alcatel.fr Tuesday, January 09, 1996 Len Heins wrote: > I wish to put a delay of 100ms in my code - are there any Windows 3.1 or MFC > functions that will allow me to do this (like the Turbo C delay() and > sleep() functions)? Here is two function which might help you : // ------------------------------------------------------ // sleep with foreground traitment of messages // ------------------------------------------------------ void nap(DWORD nap_length) { DWORD start = GetTickCount(); DWORD stop = start + nap_length; DWORD cur_time; TRACE("nap: start %ld\nstop %ld\n", start, stop); if (start < stop) { // 0----[-----X------]-----MAX // start stop do { purge_messages(); cur_time = GetTickCount(); } while ((cur_time < stop) && (cur_time >= start)); } else { // 0----]-----------[---X--MAX // or // 0-X--]-----------[------MAX // stop start do { purge_messages(); cur_time = GetTickCount(); } while ((cur_time < stop) || (cur_time >= start)); } TRACE("nap : wake up\n"); } and to let applications working during our nap void purge_messages() { MSG msg; while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message != WM_QUIT) { TranslateMessage(&msg); DispatchMessage(&msg); } } } And if you don't want to give up time to other applications during your nap, just don't call purge_messages() but as you said "this is not a great idea in a cooperative multitasking environment" Good luck -- a poor lonely frenchie --
LeRoy Baxter -- lbaxter@cinfo.com Wednesday, January 10, 1996 Do you still need the PeekMessage() for 32-bit Windows (Win95/WinNT) ? I was under the impression they had their own message queues and it would no longer lock up all of (32-bit) Windows. I think you can also use: GetTickCount() ---------- From: Brad Wilson[SMTP:bradw@netnet.net] Sent: Monday, January 08, 1996 4:49 PM To: 'MFC Discussion List' Subject: Re: Simple delays and local time. >> I wish to put a delay of 100ms in my code - are there any Windows 3.1 or MFC >> functions that will allow me to do this (like the Turbo C delay() and >> sleep() functions)? No. You can cobble one together for yourself, though, like this one: DWORD msEnd = timeGetTime( ) + 100; // 100ms while( msEnd < timeGetTime( )) { // Do nothing! } (NOTE: timeGetTime() is a multimedia API function, are requires that you include MMSYSTEM.H and link with MMSYSTEM.LIB) That will completely lock up Windows 3.1 for 100 milliseconds. If you want to sleep for 100ms but let the system live during that time (HIGHLY recommended), you can do this: DWORD msEnd = timeGetTime( ) + 100; // 100ms MSG msg; while( msEnd < timeGetTime( )) { // Dispatch messages while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } That's a very useful function to have (all caveats regarding PeekMessage() loops apply, so don't make your delays too long). You'll have to do some extra work if your application cannot handle re-entry because of message delivery, as well as if the main window that is displayed is a dialog box (ie, call IsDialogMessage() too). Good luck! Brad -- class CBradWilson : public CWorldWatchProgrammingTeam { public: CString GetInetAddr() { return CString("bradw@exptech.com"); } CString GetPhone() { return CString("+1 (810) 620-9803"); } CString GetURL() { return CString("http://www.exptech.com"); } CString GetDisclaimer() { return CString("All I say is fact :-p"); } }; "When you look into these eyes, do you see civilized, or Son of Sam? Will you love or do you hate? Why do you hesitate?"
David W. Gillett -- DGILLETT@expertedge.com Thursday, January 11, 1996 > Do you still need the PeekMessage() for 32-bit Windows > (Win95/WinNT) ? I was under the impression they had their own > message queues and it would no longer lock up all of (32-bit) > Windows. It's true that you no longer have to call PeekMessage for other apps' -- or other threads' -- messages to get dispatched. But if this thread owns some windows, you might want to call PeekMessage for them. And if not, busy-waiting on GetTickCount or timeGetTime does still get scheduled and use CPU time that would be available to other threads if you blocked on a synchronization object (such as a semaphore or a mutex). Dave
Brad Wilson -- bradw@netnet.net Thursday, January 11, 1996 >> Do you still need the PeekMessage() for 32-bit Windows (Win95/WinNT) ? The original author stated Visual C++ 1.5x. >> I was under the impression they had their own message queues and it would >> no longer lock up all of (32-bit) Windows. Threads and such. :-) >> I think you can also use: GetTickCount() It's not as accurate. If you need sleeps of any less that 55ms, or moderately accurate sleeps, timeGetTime() works best. Brad -- class CBradWilson : public CWorldWatchProgrammingTeam { public: CString GetInetAddr() { return CString("bradw@exptech.com"); } CString GetPhone() { return CString("+1 (810) 620-9803"); } CString GetURL() { return CString("http://www.exptech.com"); } CString GetDisclaimer() { return CString("All I say is fact :-p"); } }; "Money is the living power that dies without its root. Money will not serve the mind that cannot match it. Is this the reason why you call it evil?" -----From: Mario Contestabile[Snip.Begin()] Do you still need the PeekMessage() for 32-bit Windows (Win95/WinNT) ? I was under the impression they had their own message queues and it would no longer lock up all of (32-bit) Windows. I think you can also use: GetTickCount() -----From: Marty Fried At 07:47 AM 1/10/96 -0800, LeRoy Baxter wrote: >Do you still need the PeekMessage() for 32-bit Windows (Win95/WinNT) ? >I was under the impression they had their own message queues and it would >no longer lock up all of (32-bit) Windows. > >I think you can also use: GetTickCount() The PeekMessage isn't necessary in win32 for yielding to other apps; but if you activate another app that obscures the original app's window(s), then activate the original app, its windows won't redraw if it isn't processing messages, which looks pretty funky. And you won't get keyboard or mouse input, if that is being watched for any reason. This is a good reason to make the UI portion a separate thread (something I haven't done yet). GetTickCount() is probably the best function to use if you don't need higher resolution. It's limited to somewhere around 30 - 40 ms or so. I forget the exact number. The multimedia timer is around 1 ms or so, I believe. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Marty Fried (mfried@linex.com) Marin County, California -----From: jerasmussen@falconholm.dk In Win 3.1(which he was talking about) it surely is necessary. I'm relatively new to Win95 and I thought that a lengthy operation wouldn't= =20 lockup the application, but I'm experiencing that it could be a problem if=20 you have a single-threaded application (i.e. the message pump resides in th= e=20 same thread as your lengthy operation - sorry Win95 gurus if I'm wrong...).= =20 This can lockup the UI of the application itself, but shouldn't affect othe= r=20 apps. as they're running in different processes. Concerning using the GetTickCount() function; You're right - he could have=20 used that function, but there is one thing you should be aware of:=20 GetTickCount() does not return successive values when called repeatedly. Th= e=20 value is only updated each 18th of a second (I think) so the values will=20 have the same value for several calls and then gaps of a bit more than 50 m= s=20 (more precisely 1000/18 ms). The timeGetTime() function does not have this inconvenience (I think it=20 would be quite difficult to make multimedia apps if you couldn't get a more= =20 precise timing...). Stay tuned, Jens-Erik Rasmussen, Falconholm International A/S, Denmark -----From: mikeblas@interserv.com On Wed, 10 Jan 1996, LeRoy Baxter wrote: >Do you still need the PeekMessage() for 32-bit Windows (Win95/WinNT) ? >I was under the impression they had their own message queues and it would >no longer lock up all of (32-bit) Windows. That _who_ had their own message queues? Each thread in Win32 can get its own message queue, but not each window. .B ekiM -- TCHAR szDisc[] = _T("These words are my own, but I used some twice.");
| Вернуться в корень Архива |