views:

64

answers:

3

hey guys I'm trying to create a timer which counts the time spent on a page using a thread heres what I have so far:

<cfset session.time=0>
<cfthread name="timer" action="run">
<cfscript>
  counter = 0;
  while (counter <9000) {
    sleep(1000);
    session.time++;
    counter ++;
} 
</cfscript>
</cfthread>

page 2:

<cfoutput>#session.time#</cfoutput>

page 2 gives me 0 every time anyone see a problem?

edit: I changed line 1 of my code to <cfset session.time=100> and now page 2 says 100, its like the stuff inside the cfscript loop isn't reassigning session.time

A: 

Your code works fine for me. You have got an Application.cfm page setup to enable session management i.e:

<cfapplication name="#hash(getCurrentTemplatePath())#"  
    sessiontimeout="#createTimeSpan(0,0,20,0)#" sessionmanagement="true"/>
Andy Jarrett
I believe this is enabled already, other session variables passed to other pages work fine.
ajoe
A: 

This works for me.

Are you sure you are using the Developer or Enterprise edition of ColdFusion and the threads have actually kicked off? I think only those editions support multi-threading.

One way to do verity your threads working is to use cfstat - you should see one request running even though your page has returned.

Another is to write output from your spawned thread - use the code snipped below to write to System.out - ideally you'll need to have CF running as a console task to do this.

<cfset session.time=0 />

<cfthread name="timer" action="run">
<cfscript>
  counter = 0;
  while (counter <9000) {
    sleep(1000);
    session.time++;

    sys = createObject("java", "java.lang.System");
    sys.out.println("*** [DEBUG]  - #timeformat(now(),'HH:mm:ss' )# - session.time=#session.time# ");

    counter ++;
} 
</cfscript>
</cfthread>
Ciaran Archer
Standard supports multithreading, but you are limited on background thread count (to 10, IIRC).
Ben Doom
A: 

Code executed inside a thread has its own scope, including session. I would set a variable within the tread and then access it from within the threads scope.

i.e. Change session.time++; to thread.time++; and then use cfthread[timer].time to get the thread's time.

Although this may not hold if you enable session management like some of the other posts discuss.

Zugwalt
any idea on how to store the last cfthread[timer].time in a session variable so I can view it on page 2?
ajoe
sorry I forgot to include the thread scope. If you create and modify the variable as an entry of thread scope within a thread, it should be accessible from anywhere. Use thread.time within the tread, and then on the second page try cfthread[threadName].variableName--no session scope required!
Zugwalt
I just tried this, it doesn't see to recognize the variable on pg 2. I simplified it just to test it out. <cfthread name="timer" action="run"><cfset Thread.time = 0></cfthread>page 2: #cfthread[timer].time#throws an Variable TIMER is undefined. error
ajoe
Wrong - session scope is shared between threads. See http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=threads_4.html, specifically 'All the threads run in a request share the same Form, URL, Request, CGI, Cookie, Session, Application, Server and Client scopes.'
Ciaran Archer
Ah yes, appologies I was mixing up my scopes with web requests, where the request executes within its own session scope.
Zugwalt