Michael Tangorre asked:
> Can anyone think of a way that I can achieve a pause for like 30 
> seconds to a minute between iterations in a loop?

This question gets asked quite a bit, so I figured I'd forward my
alternative.

  <CFSET Sleep=30>
  <CFSET Start=GetTickCount()>
  <CFTRY>
  <CFLOCK SCOPE="SESSION" TIMEOUT="#Sleep#" TYPE="READONLY">
    <CFLOCK SCOPE="SESSION" TIMEOUT="#Sleep#" TYPE="EXCLUSIVE">
    </CFLOCK>
  </CFLOCK>
  <CFCATCH></CFCATCH>
  </CFTRY>
  <CFSET End=GetTickCount()>
  <CFSET Elapsed=End-Start>
  <CFOUTPUT>Elapsed: #Elapsed#ms</CFOUTPUT>

It's nicer points are:

1. It doesn't require anything external to be installed (CFX tags, Java,
etc).  (It does, however, require that you have session management
enabled.  If you don't have session management enabled, then the code
doesn't sleep.)
2. No spiking the processor.

The drawbacks are:

1. It will hold up all locks in the *Session*, so don't use it if you
expect the user to be doing other things while the thread sleeps.
2. Since it uses Session-scoped locking you'll need CF 4.5.

If what it is doing is non-obvious, here's a quick explanation:

- The first CFLOCK statement creates a Session lock.
- The second CFLOCK tries to get a lock, but it can't because there is
already a lock.  (CF doesn't seem to care that the thread owns the lock
it is waiting on.)  Classic deadlock.
- It sleeps (actually a WaitForSingleObject() call, I assume, which is
basically the same thing) until the TIMEOUT has been reached, at which
point it throws an error.
- The surrounding CFTRY/CFCATCH statement traps the error, ignores it,
and the thread continues on.

This leads to drawback 3:

3. If MacroAllaire ever gets around to making the locking mechanism
smart enough to realize that a thread already has a lock and just throw
it out, this trick will cease to work.

In case you are wondering if this would work with Named locks (and
therefore 4.0 and not tie up Session locks), I don't have a definitive
answer for you.  I messed around with it a bit, but couldn't get it to
work consistantly.  However, since normally if you want a Session thread
to sleep you don't tend to mind if the Session locks, I haven't bothered
to do much testing.

HTH,

-R


______________________________________________________________________
Get the mailserver that powers this list at http://www.coolfusion.com
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to