Vex::task scope

In VexCode text, when I declare a vex::task object, does the task automatically stop when the object goes out of scope?

No.

3 Likes

Is there a way to stop all tasks like tgere was in ROBOTC?

This was the line of code I found on the API
vex::task::stop ( const task & t )

1 Like

Not as such at the moment.

Individual tasks can be stopped, if you created a task like this.

task mytask( mycallback );

That task can be stopped by using

mytask.stop();

but there are other ways as well, it can be stopped by using the name of the callback.

task::stop( mycallback );

or the task instance

task::stop( mytask );

tasks behave much as ROBOTC tasks did, students have all sort of bad usage patterns, treating them like functions etc. so, for example, constantly creating a task in a loop will simply keep starting that same task from the beginning.

So what’s the problem with a stop all method ?
The issue is that events are also tasks, when you create an event such as

Controller1.ButtonA.pressed( mybuttoncallback );

the function triggered by the event is running as a task, knowing what to do when a “stop all” method was called is a bit ambiguous, so we leave it to the user code to track tasks and events and decide the best course of action.

There is, however, one situation we can handle in a competition environment. The competition class has a flag that can be set called bStopAllTasksBetweenModes. setting this to true will cause the competition control to reset everything to the situation the code was in when main was called when the robot is disabled, it will kill any tasks and event created after that point, tasks that are created as part of global constructors (which run before main is entered) will remain, it’s intended as pretty advanced control so you need to really understand what the implications of using that are.

2 Likes

You could achieve similar functionality to ROBOTC’s stopAllTasks by maintaining a list of all the tasks you start, something like this:

#include <vector>

vector<task> allTasks;

void stopAllTasks(){
    for (int i=0; i<allTasks.size(); i++){
        allTasks.front().stop();
        allTasks.erase(allTasks.front());
    }
}

int main(){
    //any time you start a task, add it to the list:
    task task1(someFunc);
    allTasks.push(task1);
    task task2(someOtherFunc);
    allTasks.push(task2);
    task task3(yetAnotherFunc);
    allTasks.push(task3);

    //now calling stopAllTasks will stop all the tasks and clear the list:
    stopAllTasks();
}
2 Likes

just FYI, todays update of VEXcode now has a “stop all tasks” class member function for task and thread.

task::stopAll();

or

thread::interruptAll();

main will not be stopped, events will not be unregistered. motors will continue to run if field control still has the robot enabled.

3 Likes