Thread Subject: Deleting timers from within their StopFcn

Subject: Deleting timers from within their StopFcn

From: Kenneth Eaton

Date: 5 Dec, 2008 20:55:06

Message: 1 of 2

Looking over the examples The MathWorks gives for using timers, I noticed that they are always deleted after being used (a good practice). However, I was curious if you could just delete a timer from within it's own StopFcn, so that it would delete itself upon completing its task. I tested it a few times and it seems to work just fine.

Since I've never seen an example of this being done, I was just curious if anyone knew of any unforeseen reason why this would be a bad thing to do?

Ken

Subject: Deleting timers from within their StopFcn

From: Kenneth Eaton

Date: 5 Dec, 2008 21:13:03

Message: 2 of 2

Never mind... I just found a situation where things go wrong. If you are in a situation where a program is waiting for a timer to complete, you get an error. Here is an example:

>> ht=timer('ExecutionMode','fixedRate','Period',1,'TasksToExecute',10,...
'TimerFcn','disp(''tick'');','StopFcn','delete(ht)');
>> start(ht); wait(ht);
tick
tick
tick
tick
tick
tick
tick
tick
tick
tick
??? No appropriate method or public field isRunning for class handle.

Error in ==> timer.wait at 46
    end

In this case, I would have to delete the timer after the WAIT command.

Ken

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
timer Kenneth Eaton 5 Dec, 2008 15:55:08
delete Kenneth Eaton 5 Dec, 2008 15:55:08
stopfcn Kenneth Eaton 5 Dec, 2008 15:55:08
rssFeed for this Thread

Contact us at files@mathworks.com