CFTHREAD JOINを使用してCFLOOPで実行されている変数の値を取得する

StackOverflow https://stackoverflow.com/questions/3248513

  •  15-09-2020
  •  | 
  •  

質問

返信してくれてありがとう!しかし、私はまだそれをすることができません。私が到着しているのは誤りです msgstr ""要素objget1は、クラスColdFusion.Runtime.variablocope型のJavaオブジェクトでは未定義です。 "

以下の

は私のフルコードです。CFHTTP情報を含む各スレッドの値をダンプしたいだけです。

http://www.google.com/search? "&" q= VIN +ディーゼル "&"&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>
.

と糸の内部に接合した後に使用するとき。私は望ましい結果を得ます ありがとう!!

役に立ちましたか?

解決

ここで起こっている2つの問題。

Zugwaltによって指摘されているように、あなたはあなたのスレッドの範囲内で参照したい変数を明示的に渡す必要があります。彼はCGI変数を見逃していた、その範囲はあなたのスレッド内に存在しない。だから私たちはスレッド、UserAgent、StrbaseURL、およびインポートで使用する必要があるものだけを渡します。

第2の問題は、結合されると、あなたのスレッドは可変の範囲内ではなく、それらは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>
.

他のヒント

一般に、非記録されていない変数はVariablesスコープに入れるので、Struct Bracket表記を使用してそれらを参照できます。

Variables['objGet#intGet#']
.

または

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top