为什么脚本不停止运行?

简介: 牙叔教程 简单易懂

牙叔教程 简单易懂


群里有人提了个问题, 为什么脚本不停止运行?

代码如下

var thread;
var timer;
toastLog("脚本开始");
const delayBeforeCloseApp = 10;
thread = threads.start(function () {
  const start = new Date().getTime();
  timer = setInterval(() => {
    let diff = new Date().getTime() - start;
    if (diff < delayBeforeCloseApp * 1000) {
      const left = delayBeforeCloseApp * 1000 - diff;
      toastLog("剩余" + Math.floor(left / 1000) + "秒关闭应用");
    } else {
      clearInterval(timer);
    }
  }, 1 * 1000);
});
sleep(10 * 1000);
thread.interrupt();


运行日志如下


可以看到, 脚本确实没有停止运行


多打日志

var thread;
var timer;
toastLog("脚本开始1");
const delayBeforeCloseApp = 10;
thread = threads.start(function () {
  const start = new Date().getTime();
  timer = setInterval(() => {
    let diff = new Date().getTime() - start;
    if (diff < delayBeforeCloseApp * 1000) {
      const left = delayBeforeCloseApp * 1000 - diff;
      toastLog("剩余" + Math.floor(left / 1000) + "秒关闭应用");
    } else {
      log("清除定时器 start");
      clearInterval(timer);
      log("清除定时器 end");
    }
  }, 1 * 1000);
});
sleep(10 * 1000);
log("关闭多线程 start");
thread.interrupt();
log("关闭多线程 end");


再次运行上面的代码, 日志如下


可以看到多线程已经关闭了, 但是预期的清除定时器并没有打印出来


把定时器运行的时间设置的短一点, 比如5秒

var thread;
var timer;
toastLog("脚本开始1");
const delayBeforeCloseApp = 5;
thread = threads.start(function () {
  const start = new Date().getTime();
  timer = setInterval(() => {
    let diff = new Date().getTime() - start;
    if (diff < delayBeforeCloseApp * 1000) {
      const left = delayBeforeCloseApp * 1000 - diff;
      toastLog("剩余" + Math.floor(left / 1000) + "秒关闭应用");
    } else {
      log("清除定时器 start");
      clearInterval(timer);
      log("清除定时器 end");
    }
  }, 1 * 1000);
});
sleep(10 * 1000);
log("关闭多线程 start");
thread.interrupt();
log("关闭多线程 end");


运行日志如下


可以看到先清除定时器, 再关闭多线程, 脚本正常停止, 符合预期


那么, 可能引起问题的原因, 就是定时器没有正常关闭,

可能多线程关闭了之后, 定时器理论上应该跟着一起关闭,

但实际上, 定时器没有关闭, 或者说没有完全关闭,

导致脚本不能正常停止运行.

那怎么解决呢?

我们在关闭线程前面, 加一句关闭定时器

var thread;
var timer;
toastLog("脚本开始1");
const delayBeforeCloseApp = 10;
thread = threads.start(function () {
  const start = new Date().getTime();
  timer = setInterval(() => {
    let diff = new Date().getTime() - start;
    if (diff < delayBeforeCloseApp * 1000) {
      const left = delayBeforeCloseApp * 1000 - diff;
      toastLog("剩余" + Math.floor(left / 1000) + "秒关闭应用");
    } else {
      log("清除定时器 start");
      clearInterval(timer);
      log("清除定时器 end");
    }
  }, 1 * 1000);
});
log("延迟10秒 start");
sleep(10 * 1000);
log("延迟10秒 end");
log("清除定时器-主线程 start");
clearInterval(timer);
log("清除定时器-主线程 end");
log("关闭多线程 start");
thread.interrupt();
log("关闭多线程 end");


上面的代码, 运行日志如下


可以看到, 清除定时器-主线程, 关闭多线程都出来了, 但是脚本就是没有停止

猜想

是不是只能在启动定时器的多线程里面, 才能清除定时器啊?

在主线程清除定时器, 有没有办法?


我们在主线程里面加个定时器, 先停止Interval定时器, 再停止多线程

var thread;
var timer;
toastLog("脚本开始3");
const delayBeforeCloseApp = 10;
thread = threads.start(function () {
  const start = new Date().getTime();
  timer = setInterval(() => {
    let diff = new Date().getTime() - start;
    if (diff < delayBeforeCloseApp * 1000) {
      const left = delayBeforeCloseApp * 1000 - diff;
      toastLog("剩余" + Math.floor(left / 1000) + "秒关闭应用");
    } else {
      log("清除定时器 start");
      clearInterval(timer);
      log("清除定时器 end");
    }
  }, 1 * 1000);
});
log("延迟10秒 start");
sleep(10 * 1000);
log("延迟10秒 end");
log("清除定时器-主线程 start");
clearInterval(timer);
log("清除定时器-主线程 end");
setTimeout(() => {
  log("关闭多线程 start");
  thread.interrupt();
  log("关闭多线程 end");
}, 1000);


上面的代码, 运行日志如下


脚本正常结束


把setTimeout换成sleep试试

var thread;
var timer;
toastLog("脚本开始3");
const delayBeforeCloseApp = 10;
thread = threads.start(function () {
  const start = new Date().getTime();
  timer = setInterval(() => {
    let diff = new Date().getTime() - start;
    if (diff < delayBeforeCloseApp * 1000) {
      const left = delayBeforeCloseApp * 1000 - diff;
      toastLog("剩余" + Math.floor(left / 1000) + "秒关闭应用");
    } else {
      log("清除定时器 start");
      clearInterval(timer);
      log("清除定时器 end");
    }
  }, 1 * 1000);
});
log("延迟10秒 start");
sleep(10 * 1000);
log("延迟10秒 end");
log("清除定时器-主线程 start");
clearInterval(timer);
log("清除定时器-主线程 end");
sleep(1000);
log("关闭多线程 start");
thread.interrupt();
log("关闭多线程 end");


上面的代码, 运行日志如下

脚本正常停止

总结

关闭多线程之前, 应该确保多线程里面的定时器都正常关闭

名人名言


思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
--- 牙叔教程


声明


部分内容来自网络
本教程仅用于学习, 禁止用于其他用途

相关文章
|
7月前
|
Python
【python脚本】执行过程中触发若干次就停止执行脚本的方式
【python脚本】执行过程中触发若干次就停止执行脚本的方式
|
Shell
设置shell脚本执行错误自动退出
设置shell脚本执行错误自动退出
794 0
|
4月前
|
Shell Perl
在Shell脚本中,检查一个进程是否正在运行
在Shell脚本中,检查一个进程是否正在运行
258 1
|
4月前
|
监控 Unix Shell
shell定时检查
shell定时检查
42 1
|
10月前
|
SQL IDE 关系型数据库
Ascent运行指南
Ascent运行指南
68 0
|
关系型数据库 MySQL Java
【问题解决】mysql数据库启动时报服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
【问题解决】mysql数据库启动时报服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
【问题解决】mysql数据库启动时报服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
|
大数据 Python
Pythpn实现多脚本处理定时运行
Pythpn实现多脚本处理定时运行
114 0
Pythpn实现多脚本处理定时运行
|
Shell
shell脚本控制程序启动停止重启
shell脚本控制程序启动停止重启
242 0
|
Kubernetes 网络协议 安全
本地k8s运行总结
本机上运行k8s的各种不同软件的对比和结构图
436 0
查看运行结果
看下列代码的运行结果 #include "pch.h" #include using namespace std; class A { public: A() { a = 1; b = 2; } pri...
863 0