2016-03-17 39 views
1

我想做以下事情,它不會拋出一個錯誤,但我不知道它是否做任何事情。有沒有辦法在Coldfusion中檢查線程狀態?檢查coldfusion線程

<cfset start = CreateDate(2005, 1, 1) /> 
<cfset stop = DateAdd("m", 1, now()) /> 

<cfloop condition="start LTE stop"> 

    <cfthread name="#dateformat(start, 'mmddyyyy')#" action="run"> 

      <cfinvoke component="CFCs.DoSomething" method="DoSomething" 
       returnvariable="success" 
       dateStartDate="#dateformat(start, 'mm/dd/yyyy')#" 
       dateEndDate="#dateformat(DateAdd('m', 1, start), 'mm/dd/yyyy')#" 
       /> 


    </cfthread> 

    <cfoutput> #LSDateFormat(start)# <br/> </cfoutput> 


    <cfset start = DateAdd("m", 1, start)> 

</cfloop> 
+0

檢查'your_thread_name.Status' –

+0

是否有明顯的原因,他們可能沒有運行? CFC電話是否合法? –

+0

服務器監視器執行此操作。 –

回答

2

首先,在你的線程您start變量可以改變,因爲它不被傳入。你需要你的內螺紋範圍應在屬性來空前絕後。這使變量線程安全。否則,如果變量在線程外部發生變化,它也會在線程內部發生變化,並可能產生意外的結果。如果你想在你的線程之外訪問它們,你的線程中可以存儲變量在THREAD範圍內。您也可以將您的代碼放入try/catch中,並將該異常存儲在THREAD範圍內,以便您可以在線程之外對其進行讀取以確定它是否出錯以及原因。

保留一個線程名稱列表,然後在循環後使用<cfthread action="join">。這告訴CF等待所有線程完成。然後您可以通過CFTHREAD範圍訪問線程。 CFTHREAD結構將使用線程名稱的鍵存儲您的線程,因此您可以將其循環。

<cfset start = CreateDate(2005, 1, 1) /> 
<cfset stop = DateAdd("m", 1, now()) /> 
<cfset threadNames = ''> 

<cfloop condition="start LTE stop"> 
    <cfset newThreadName = dateformat(start, 'mmddyyyy') > 
    <!--- add thread name to list of threads ---> 
    <cfset threadNames = listAppend(threadNames, newThreadName) > 

    <cfthread name="#newThreadName#" action="run" start="#start#"> 
     <cftry> 
      <!--- store a variable in THREAD scope to be used outside thread ---> 
      <cfset THREAD.start = ATTRIBUTES.start> 
      <cfset THREAD.foo = "bar"> 

      <!--- Do stuff here ---> 

      <cfcatch type="any"> 
       <!--- catch any error and store in the thread result ---> 
       <cfset THREAD.exception = CFCATCH> 
      </cfcatch> 
     </cftry> 



    </cfthread> 

    <cfoutput> #LSDateFormat(start)# <br/> </cfoutput> 


    <cfset start = DateAdd("m", 1, start)> 

</cfloop> 

<!--- this waits for all threads to complete --->  
<cfthread action="join" name="#threadNames#" /> 

<!--- loop over thread results ---> 
<cfloop collection="#CFTHREAD#" item="t"> 
    <!--- do whatever you want with the thread result struct ---> 
    <cfoutput>#CFTHREAD[t].STATUS# <br /></cfoutput> 
</cfloop> 

<!--- Dump all the threads ---> 
<cfdump var="#CFTHREAD#" abort="true" />