Coldfusion 使用cfthread join获取在cfloop中运行的变量的值

Coldfusion 使用cfthread join获取在cfloop中运行的变量的值,coldfusion,cfloop,cfthread,Coldfusion,Cfloop,Cfthread,谢谢回复!!但我还是做不到。我得到的错误是 “元素objGet1在类型为coldfusion.runtime.VariableScope的Java对象中未定义。” 下面是我的完整代码。我只想转储包含cfhttp信息的每个线程的值 http://www.google.com/search?“&”q=Vin+Diesel“&”&num=10“&”&start=“)/> 当我在循环中使用后线程连接时,我得到了期望的结果 谢谢!!一般来说,非作用域变量会被放入变量的作用域中,因此您可以使用结构

谢谢回复!!但我还是做不到。我得到的错误是 “元素objGet1在类型为coldfusion.runtime.VariableScope的Java对象中未定义。”

下面是我的完整代码。我只想转储包含cfhttp信息的每个线程的值

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



当我在循环中使用后线程连接时,我得到了期望的结果
谢谢!!

一般来说,非作用域变量会被放入
变量的作用域中,因此您可以使用结构括号表示法来引用它们:

Variables['objGet#intGet#']


它们基本上都在做相同的事情-只是语法不同。

在cfthread标记中运行的代码有自己的作用域。尝试将您希望它访问的变量作为属性传递。我想给它命名一些不同的名称,以帮助我跟踪

<!--- 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>



这里发生了两个问题

正如Zugwalt所指出的,您需要显式地传入要在线程范围内引用的变量。他遗漏了CGI变量,该范围在线程中不存在。因此,我们只传入我们需要在线程、userAgent、strBaseURL和intGet中使用的变量

第二个问题,一旦加入,线程就不在变量范围内,它们在cfthread范围内,所以我们必须从那里读取它们

更正代码:

<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>


Hmmm,你能确认如果你把它放在第一个变量中,它是否会转储它吗?另外,试着将
cfthread
中的
名称设置为
“variables.objGet#intGet#“
-应该没有必要,但我还不需要使用cfthread,所以不能完全确定它的行为。很好!我没有仔细研究整个问题,但幸运的是你真的解决了!
<!--- 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>
<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>