TcMenu / TaskManagerIO

A task, event, scheduling, and interrupt marshalling library for Arduino and mbed boards.
Apache License 2.0
124 stars 14 forks source link

One time event (long schedule) #44

Closed lablabla closed 2 years ago

lablabla commented 2 years ago

Hi,

I have a need for a TmLongSchedule but it should only run once. Is there a reason for not having such option?

Currently I'm thinking of doing something like this. Storing the created task id, and inside the lambda function, retrieve it and cancel the task

auto taskId = taskManager.registerEvent(new TmLongSchedule(next_run*1000, [this, &event]() {    
    taskid_t taskIdToCancel = this->getTaskId(event.id);
    taskManager.cancelTask(taskIdToCancel);

    // do task after long wait
}), true);

if (taskId == TASKMGR_INVALIDID)
{
    log_e("Failed scheduling task");
    return;
}
m_jobs[event.id] = taskId;

It seems to work but it seems like a relatively hacky way of doing this, considering for "short" schedule there exists a scheduleOnce

Is there a better way of doing this that I'm missing?

Thanks for this great project!

davetcc commented 2 years ago

Examine if this is fairly straightforward and add to the current release if so.

davetcc commented 2 years ago

Actually, this literally turns out to be a oneliner, I've added a defaulted extra parameter to the constructor called oneTime

davetcc commented 2 years ago

Will be released as soon as testing is concluded on a range of boards.

davetcc commented 2 years ago

https://www.thecoderscorner.com/products/arduino-libraries/taskmanager-io/task-manager-scheduling-guide/#scheduling-tasks-more-than-an-hour-in-duration

lablabla commented 2 years ago

Amazing. Hopefully I can get around to using it this weekend Thanks a lot for the quick support!