Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

16
LINES

< > BotCompany Repo | #1032668 // doEvery_stopOnException - install a java.util.Timer, stop it when executed code throws an exception

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9886L/54K).

static FixedRateTimer doEvery_stopOnException(double initialSeconds default delaySeconds, double delaySeconds, Runnable r, Runnable onStop default null) {
  if (r == null) null;
  
  FixedRateTimer timer = new(shorten(programID() + ": " + r, 80));
  Runnable r2 = r {
    try { r.run(); } catch print e {
      print("Cancelling timer due to error.");
      timer.cancel();
      pcallF(onStop);
    }
  };
  timer.scheduleAtFixedRate(
    smartTimerTask(r2, timer, toMS_int(delaySeconds)),
    toMS_int(initialSeconds), toMS_int(delaySeconds));
  ret timer;
}

Author comment

Began life as a copy of #1004683

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032668
Snippet name: doEvery_stopOnException - install a java.util.Timer, stop it when executed code throws an exception
Eternal ID of this version: #1032668/5
Text MD5: 736186ecb0bddff4ecdae0c9cfb2e0b4
Transpilation MD5: f673c9470abc9cf2b6db0049afbe51ed
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-20 17:37:43
Source code size: 575 bytes / 16 lines
Pitched / IR pitched: No / No
Views / Downloads: 168 / 247
Version history: 4 change(s)
Referenced in: [show references]