Search This Blog

Friday, September 5, 2014

ColdFusion: Using CFTHREAD

<cfthread>...</cfthread> was introduced in ColdFusion 8; and I had not got chance to use it until recently. Basically, cfthread is used for creating asynchronous/independent processes aside from page-level processing. Here are my notes from playing with the tag and reading CF documentations.

  • thread-local scope (local scope in thread): Define the variables with "var" or without specifying a scope name. It is best practice if we specify var to create thread-local-scope variables.
  • thread scope: Define the variables with prefix thread. followed by variableName. The thread scope also contain metadata such as elapsedTime, name, status, etc.
    To call thread-scope variables from page thread: threadName.variableName
    To call thread-scope variables from other thread: cfthread.threadName.variableName
  • attributes scope: Define via cfthread attribute (<cfthread variableName="value">) or attributeCollection
  • threadName.error: Exist when there is an error. Use structKeyExists(threadName, "error") to check an error.

Reference: Using thread data