Note: This blog post is from 2007. Some content may be outdated--though not necessarily. Same with links and subsequent comments from myself or others. Corrections are welcome, in the comments. And I may revise the content as necessary.Ever wanted to pause a currently running request? You can now, in CF8, using the new sleep() function. CF8's chock full of hidden gems. Indeed, I make note of nearly 50 of them in a user group talk I've started doing. One of them is this new Sleep() function.
Some will recognize it as an easier way to call the java thread.sleep method (as many have noted, and I wrote about back in 2002). It's been added primarily as part of the multi-threaded processing (CFTHREAD) feature, such as when one thread needs to wait upon another.
But it can be useful sometimes on its own, such as when you want to simulate a long-running request for any reason. (And it's a whole lot more server-friendly than doing a huge cfloop, since a sleep call doesn't really spin the CPU. It literally halts the current request, putting it to "sleep".)
As with the sleep method in java, sleep() takes a number in milliseconds, so for 5 seconds, use 5000. You can use it in CFSCRIPT, or in a simple CFSET:
Why might you want to simulate a long-running request? There are many reasons. Perhaps to test some logic in how CFLOCK failures work, or to cause a page to appear in the new CF8 server monitor--or one of the long-existing monitor tools (to make sure you're seeing what you think you should be seeing).
Still another reason to use it has to do with another hidden gem, this one in CF7. I'll write about that shortly.
For more content like this:
Need more help with problems?
- Signup to get my blog posts by email:
- Follow my blog RSS feed
- View the rest of my blog posts
- View my blog posts on the Adobe CF portal
- If you may prefer direct help, rather than digging around here/elsewhere or via comments, I can help via my consulting services
- See that for more on how I can help a) over the web, safely and securely, b) usually very quickly, c) teaching you as we go, and d) with satisfaction guaranteed