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?



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


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.


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++){

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

    //now calling stopAllTasks will stop all the tasks and clear the list:

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




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