You are here:
Adobe MAX - The premier Adobe event! Adobe MAX - The premier Adobe event!

Force Coldfusion to Sleep controlling the Thread

I ran into an annoying problem today, that was eventually solved by forcing the current processing thread to sleep.

The basic task was to stop a windows service on the server, perform some deletions, do some copying, then restart the service.

After issuing the <cfexecute> command to stop the service on the server, I would then receive a ColdFusion error when trying to delete a folder that the service still had locked. Whilst this had me stumped for a good hour or so, I finally realised that the deletion event was occurring too quickly after the service had been stopped and that the service still had a lock on the folder and files through the Windows OS.

So I needed a way to pause the current processing thread, before continuing, allowing the locks to be removed. I found this solution on Peter Freitag's blog post “How to make ColdFusion MX go to sleep. The solution is as follows ...

<cfset thread = CreateObject("java", "java.lang.Thread") />
About to sleep for 5 seconds
<cfflush>
<cfset thread.sleep(5000)>
Done sleeping.
<cfset thread = "" />

Very simple code, but done the trick and works a treat!