5

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

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

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>

当我在循环内加入线程后使用时。我得到了想要的结果谢谢!!

4

3 回答 3

6

这里发生了两个问题。

正如 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>
于 2010-07-15T18:38:29.703 回答
3

通常,未作用域的变量会被放入Variables作用域中,因此您可以使用 struct 括号表示法来引用它们:

Variables['objGet#intGet#']

或者

Variables['objGet'&intGet]

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

于 2010-07-14T17:11:32.790 回答
0

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

于 2010-07-15T18:01:00.830 回答