#timer already canceled

22 messages · Page 1 of 1 (latest)

fickle crown
#

as u can see in the file, i am scheduling two tasks using TimerTask, and in the first line of the code in each task, u will see that i am returning if huntEnded is true, and while that works fine, the reason i am doing it like that is because, when i set the huntEnded to true and try to do timer.cancel() i just get a Timer already canceled, and i dont understand, why? i am not even trying to cancel while the task is being executed
(sorry but i cant provide any error messages, i had this issue a while ago and replicating the thing will be painful but it basically just said timer already cancelled and pointed to the line where i have the timer.cancel())

languid ravineBOT
#

This post has been reserved for your question.

Hey @fickle crown! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fickle crown
#
timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(huntEnded) return;

                if(!PokeHunt.config.isCoolDownEnabled()) {
                    startNewHunt();
                } else {

                                if (PokeHunt.config.isIndividualHunts() && PokeHunt.config.isSendHuntEndMessage()) {

                                    Objects.requireNonNull(PokeHunt.server.getPlayerList().getPlayer(owner)).sendSystemMessage(
                                            Component.literal(
                                                    Utils.formatPlaceholders(PokeHunt.language.getEndedHuntMessage(), null, pokemon)));

                                } else if (PokeHunt.config.isSendHuntEndMessage()) {

                                    Utils.broadcastMessage(
                                            Utils.formatPlaceholders(PokeHunt.language.getEndedHuntMessage(), null, pokemon));

                    }

                    huntEnded = true;
                    addToEndTime(PokeHunt.config.getCoolDown(strRarity)*60*1000);
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            startNewHunt();
                        }
                    }, PokeHunt.config.getCoolDown(strRarity)*60*1000);
                }
            }
        }, duration);



timer.schedule(new TimerTask() {
    @Override
    public void run() {
        if(huntEnded) return;

        startNewHunt();
        if(!isOwnerOnline()) {
            huntEnd(Cause.OWNER_OFFLINE);
        }
    }
}, duration + PokeHunt.config.getCoolDown(strRarity)*60*1000 + PokeHunt.config.getSafetyDelay);

broken carbon
#

How are you calling cancel?

fickle crown
#

it was smth like this

public void setCaught() {
    timer.cancel();
    huntCaught = true;
    }
#

if thats what ur asking about

broken carbon
#

so you attempt to cancel the whole timer?

#

not a specific task?

fickle crown
#

yep...

white turret
#

u rly shouldnt be trying to weave tasks together like that ..........

#

and u shouldnt use anonomous classes like that

#
public class PokeHunt {
  private static final int COOLDOWN_DURATION = 60*1000;

  private void scheduleNextHunt() {
    timer.schedule(
          this::startNewHunt,
          strRarity * COOLDOWN_DURATION,
          duration);
  }
}

u dont need anonomous classes in modern java
since TimerTask only has 1 method its automatically a functional interface and u can use it that way

#

also, ur method calls should be neater so they readable like this

broken carbon
#

tbh I don't like Timer, I prefer ScheduledExecutorService lol

white turret
#

and replace the magic numbers

white turret
#

but Timer is specifically for javaFX iirc
so by using it u end up in the GUI event queue, which is a benefit

fickle crown
#

i meaaaaaan

fickle crown
#

im using Timer cause i wrote Time and intelliJ gave it to me

#

gonna take a look at ScheduledExecutorService