Pedro Elias
Pedro Elias

Reputation: 23

Stop() Timer in currently executing

I saw in another question regarding swing Timer API and your methods, preference stop(); my question is, is it possible to stop (cancel if util.timer) a timer when it's currently executing?

I have a timer and need to stop a thread when I call stop(), even if it is currently running.

Upvotes: 0

Views: 440

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Yes, it is possible to stop a Swing Timer by calling stop(). That's why this class has this method. Having said this, I have a feeling that there is more to your problem then you are telling us. But I don't know what a Swing Timer has to do with "stopping threads".

Edit
You state:

my problem is, I have a swing timer and this execute another single method, that execute every 30 seconds, and I can execute the same single method manually, by the way, I want to stop swing timer when the manually method is running and when the manually method is finished, swing timer is started, because the timer can hinder other method. The main is the manually method.

One possible solution: You could have the Swing Timer, but just enclose most of its code in an if statement controlled by a boolean say called, runTimer, so that the code in the Timer's actionPerformed only runs if the boolean is true. Then run your background thread in a SwingWorker, but set the runTimer boolean to false before executing the SwingWorker, and then set it to true in the SwingWorker's done() method.

Upvotes: 3

Related Questions