Get value of variables running in cfloop using cfthread join

2.4k Views Asked by At

Thanks for replying!! But I am still not able to do it. Error that I am getting is "Element objGet1 is undefined in a Java object of type class coldfusion.runtime.VariableScope."

Below is my full code. I just want to dump the value of each thread containing cfhttp information.

http://www.google.com/search?" & "q=Vin+Diesel" & "&num=10" & "&start=") />

<cfset intStartTime = GetTickCount() />

<cfloop index="intGet" from="1" to="10" step="1">

    <!--- Start a new thread for this CFHttp call. --->
    <cfthread action="run" name="objGet#intGet#">

        <cfhttp method="GET" url="#strBaseURL##((intGet - 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGet#" />

    </cfthread>

</cfloop>

<cfloop index="intGet" from="1" to="10" step="1">

    <cfthread action="join" name="objGet#intGet#" />
    <cfdump var="#Variables['objGet'&intGet]#"><br />

</cfloop>

and when I use after thread joining inside the loop. I get the desired results Thanks!!

3

There are 3 best solutions below

1
On BEST ANSWER

Two problems happening here.

As pointed out by Zugwalt, you need to explicitly pass in variables that you want to reference within the scope of your thread. He missed the CGI variable, that scope doesn't exist within your thread. So we pass in just what we need to use in the thread, userAgent, strBaseURL, and intGet.

Second problem, once joined, your threads are not in variable scope, they are in the cfthread scope, so we have to read them from there.

Corrected code:

<cfloop index="intGet" from="1" to="2" step="1">

    <!--- Start a new thread for this CFHttp call. Pass in user Agent, strBaseURL, and intGet --->
    <cfthread action="run" name="objGet#intGet#" userAgent="#cgi.http_user_agent#" intGet="#intGet#" strBaseURL="#strBaseURL#">

        <!--- Store the http request into the thread scope, so it will be visible after joining--->
        <cfhttp method="GET" url="#strBaseURL & ((intGet - 1) * 10)#" userAgent="#userAgent#" result="thread.get#intGet#"  />

    </cfthread>

</cfloop>

<cfloop index="intGet" from="1" to="2" step="1">

    <!--- Join each thread ---> 
    <cfthread action="join" name="objGet#intGet#" />
    <!--- Dump each named thread from the cfthread scope --->
    <cfdump var="#cfthread['objGet#intGet#']#" />

</cfloop>
1
On

Generally, unscoped variables get put into the Variables scope, so you can use struct bracket notation to refer to them:

Variables['objGet#intGet#']

or

Variables['objGet'&intGet]

These are both basically doing the same thing - just different syntaxes.

0
On

Code run inside a cfthread tag has its own scope. Try passing the variable you want it to access as an attribute. I like to name it something different just to help me keep track.

<!--- Start a new thread for this CFHttp call. --->
<cfthread action="run" name="objGet#intGet#" intGetForThread="#intGet#">

    <cfhttp method="GET" url="#strBaseURL##((intGetForThread- 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGetForThread#" />

</cfthread>