Im using yield and sleep(x) inside a loop to hand back cpu time to background processes.
Problem is Yield hands back to the OS without sleeping and sleep(x) just puts the thread to sleep without handing back to the OS.
Problem is Im starting lots of new threads which do little jobs. If I use Sleep(x) before yield, they all sleep but theres no guarantee the background apps/processes will get the extra cpu time. This is made worse if Ive started a thread with a higher priority as the MS docs suggest the lower priority threads wont get any extra cpu time.
So does anyone know of an api which combines Sleep(x) and yield?
In C# there is a spinwait method, but I found there is a SleepEx api which can put a thread to sleep for a period of time or infinite. When the thread is set to sleep for infinite it can be woken up with a few alerts. One being ReadFileEx which can send a WAIT_IO_COMPLETION event to wake the thread.
This is the specifics with links to the sleepex and readfileex api’s.
I’m pretty sure there’s been serious skepticism as to whether YIELD does anything at all. I have found that using a Window’s EVENT:Timer and Notify seem to work best at avoiding the dreaded “unresponsive” effect.
It only works on Intels with hyperthreading and from server 2003 or from vista workstation.
Its only one line of code, I think sleep is still the best way forward for background processes to have some cpu time. But now theres a sleepex api which might be the suspended threads seen in the taskmanger. I wont know for sure until I try sleepex.
Not sure what you’re doing, but you might find Capesoft GUTS to be useful for communicating between objects in your app. It can help dealing with timing issues, and remove at least some of the need for SLEEP().
Although you have to place an order for it, there is no cost.
Ive got pipes working here so I can tap into other apps, but guts looks like a global threadmanager tracking class references with an additional grouping called ‘family’. That could be handy for setting properties in a class on another thread, but it leaves me wondering why the need?
RchdR,
Do you have CapeSoft’s WinEvent?
I just added a call to its ds_sleep() function after my reports issue a CLOSE(Report) and the report was being written to PDF. I had found the PDF file was not available right away after the CLOSE(Report). I needed it to be available, so I could open it in the user’s browser or PDF viewer. Calling ds_sleep(200) causes the program to wait 2 seconds before it tries to open the .PDF and that seemed to work on my limited testing. But, I have no idea if that will work on production servers where the application is running under Terminal Services. Do you know if ds_sleep() behaves the way you want it to?
ds_sleep is just a wrapper around the OS API Sleep procedure that allows you to specify the number of hundreds of seconds to sleep. The OS API Sleep procedure requires number of milliseconds.
I used to have winevent I dont now, but I suspect its based on sleep if there is no option to alert the thread.
The suite mask in VersionInfoExA called from GetVersionExA needs ver_suite_terminal and not ver_suite_singleuserTS to detect a terminal server running in application mode.
Ie
If Band(val,ver_suite_terminal) and not Band(val,ver_suite_singleuserTS)
!application mode terminal server
End
Im doing all this on my phone atm so excuse the lack of formatting.
Sleep and sleepex works across all versions of windows workstation and server.
Its very similar to a loop with sleep, but its opening what would be a hidden window so you still see the window flash on slow pc’s, which I’d like to avoid.
The timer and sleep can both be adjusted, so its very similar, but with the overhead of the window.
And if I were to add field controls to that window, despite the window being hidden, I can extract the values from those controls using something like Spy++ or my own code.
So far since 8.1 or 10, parts of the OS are now off limits to Spy++ like the Settings window, the replacement for control panel, but I have been able to extract info from it with my own code.
So where M$ have a note in that link re usefullness of Spy++ and dotnet apps, thats abit misleading if not using my win32 api code to get at that info.
The sleepex with infinite and passing callback io completion is probably the most efficient, but I cant help but wonder if Im justing shifting my loop/sleep code back into M$ own version of a loop/sleep.