bStopAllTasksBetweenModes

hello there

Excuse me if this post kind of sucks, I’m about to go to sleep
So I want to make a timer for usercontrol since i want to use certain actions when the timer’s at specific intervals. I have the timer working pretty well so far, however, I want it to instantiate only once during usercontrol so I can get the timing consistent. Unfortunately, the way we have our auton selector means usercontrol will always run twice during a match (see this thread). Besides this task, I am also using event handlers in the form of Controller.Button_.pressed(); All event handlers are in main. Would the Competition.bStopAllTasksBetweenModes flag interfere with the event handlers or the task itself? I can send code if necessary.

not sure I completely follow what you are asking, but when you create a vex::timer it will be initialized to 0, so I’m not sure if it matters if you create one every time usercontrol is run.

I didn’t initialize the vex timer, I ended up making my own thread for it.

int clock_task() {
  static int clock;
  static int minutes;
  static int seconds;
  while (1) {
    clock++;
    seconds = clock - (minutes * 60);
    if (seconds >= 60) {
      minutes++;
      seconds = 0;
    }

    if (seconds < 10) {
      time_to_print = intToString(minutes) + ":" + "0" + intToString(seconds);
    } else {
      time_to_print = intToString(minutes) + ":" + intToString(seconds);
    }
    if (isAutonSelected) {
      printQOLS();
    }
    this_thread::sleep_for(1000);
  }
}

I do this since I only want to execute time in seconds.

If I instantiate the thread at the beginning of usercontrol, will using the bStopAllTasksBetweenModes flag remove the possibilites of accidentally duplicating the task, and will it interfere with my event handlers?

if it would interfere with my controller handlers, should I move those to usercontrol as well?

@jpearman if I used the stopAllTasksBetweenModes flag and moved my event handlers to usercontrol, would the clock initialize once?

The bStopAllTasksBetweenModes flag will attempt to stop all tasks that have been started since you entered main, this will include all event handlers as, on V5, every event handler has an associated task that is blocked waiting on a semaphore. I don’t think that’s the right solution if I understand your requirement.

If starting that task (task and thread are basically the same thing) at the beginning of user control, and you want the timer to start at zero each time, then just change the type of your variables. You had them as static, that means they would have been initialized when the program started, there’s really no need to do that unless you were trying to create a timer that remembered its state when started for a second time.

int clock_task() {
  int clock   = 0;
  int minutes = 0;
  int seconds = 0;

starting the task a second time will not create a second task (long explanation I’m not going to go into here) but simply restart that function from its entry point. The variables will be initialized back to 0, and your timer will I think behave as you need.