What would prevent a timer from running?

I’m really stumped on this one… I’ve created small simple projects with timers that fire as they should, no problems.

But in my larger test project (that connects to a PostGreSQLDatabase in threads) I cannot get a Timer to fire, even when setting the period and mode to other than “off” and yielding to other threads in the DB threads.

What would prevent a timer’s .action event from firing?

I have a Window with a button that when clicked calls a method in the Window that calls a method within a Module. The module method has a local property that is of a Timer subclass that I created. I set the Timer property to a new myTimerClass, then set the .period and the .mode to Timer.ModeMultiple. The timer never fires after this point.

Right after setting up the Timer I create a new thread and run it, then have a while loop that waits for the thread to finish running and does an ‘App.SleepCurrentThread(250)’:

pRunningThreadRef.Run // Start the thread
  
  while ( pRunningThreadRef.State = Thread.Running )
    
    App.SleepCurrentThread(250)
        
  wend

I’ve put in break points in the Timer’s .action event and the IDE never breaks there. The code in the Timer’s action event should be calling a method in a window so that it can update it’s UI. (Yes, I’ve read all of the many threads on threads and timers for updating UI’s, I promise :wink:

As a lame/weak work-around I added the same code that was in the Timer’s .action event to the above while loop:

  pRunningThreadRef.Run // Start the thread
  
  while ( pRunningThreadRef.State = Thread.Running )
    
    App.SleepCurrentThread(250)
    
    if ( pRunningThreadRef.pThreadRunningStatus <> "" ) then
      
      paramWindowToUpdateUI.UpdateUI( pRunningThreadRef.pThreadRunningStatus )
      
      pRunningThreadRef.pThreadRunningStatus = "" // Clear the status
      
    else
      
      paramWindowToUpdateUI.UpdateUI( "" ) // Still need to refresh the window
      
    end if
    
  wend

I’ve moved the Timer subclass to the root of my project versus in the Module. No change. I’ve even tried the version of code that Norman suggested with creating a Timer in the thread’s .run event and used AddHandler to create a delegate for the timer’s action event, but again the Timer does not fire.

I hate to admit that I’m at a loss on this one.

Any suggestions or thoughts on how to debug or fix this?

That blocks the main thread and prevents the timer from firing. You should never have a loop that “waits” for anything. Try to embrace an event-driven programming model.

Any event handler (which includes Button.Action, MenuHandlers, Timer.Action, etc) will block the event loop while until it returns. In other words:

Pushbutton1.Action
  for i = 1 to 1000000
    ' waste some time
  next

No other events can be processed while the pushbutton is in the for loop. Threads, however, will be handled during this time.

So, my guess is that most likely you are failing to return from the PushButton event handler. Thus, no other event handlers can fire. If true, you’ll need to refactor your code.

If this is not the answer, then a second possibility is that the Timer you are creating is going to Nil because there is no global reference to it.

Thanks guys! I’ll dig in and investigate further on these tips.

If you are waiting for the thread to finish, why have a thread at all?

Alternative: Have a Timer check the status of the Thread and do something when it finishes.

Better alternative: In your Thread subclass, create a “Finished” event that you can use to start your Timer (or something else that doesn’t involve the UI directly).

Thanks Kem. I was just about to ask what’s the better way to wait for a thread to finish versus the busy waiting loop.