B2B网络软件

标题: 苹果JS代码运行时[selfRunTime]小结 [打印本页]

作者: YYPOST群发软件    时间: 13 小时前
标题: 苹果JS代码运行时[selfRunTime]小结
苹果JS代码运行时[selfRunTime]小结
苹果JS代码运行时[selfRunTime]小结 B2B网络软件
苹果JS代码运行时[selfRunTime]小结 B2B网络软件
苹果JS代码运行时[selfRunTime]小结 B2B网络软件

  1. /**
  2. * AIWROK 软件苹果开发文档 - stop 和 runOnUIThread 方法详解
  3. * AIWROK软件安卓交流QQ群711841924
  4. * 苹果内测软件QQ群648461709
  5. */

  6. /**
  7. * 方法一:stop(停止运行)详解与示例
  8. * 功能:停止当前运行的程序或脚本
  9. * 参数:无
  10. * 返回值:无(Void)
  11. */

  12. // 示例场景1:条件检测与程序终止
  13. function example1_conditionBasedStop() {
  14.     printl("=== 示例1:条件检测与程序终止 ===");
  15.    
  16.     // 模拟一个循环任务
  17.     for (let i = 0; i < 100; i++) {
  18.         printl(`执行任务 ${i}/100...`);
  19.         
  20.         // 模拟一些处理时间
  21.         sleep.second(0.5);
  22.         
  23.         // 检测终止条件
  24.         if (i === 10) {
  25.             printl("⚠️  检测到异常条件,准备终止程序...");
  26.             // 执行清理工作
  27.             printl("&#128260;  正在执行清理操作...");
  28.             sleep.second(1);
  29.             printl("✅  清理完成,程序终止");
  30.             
  31.             // 停止程序运行
  32.             selfRunTime.stop();
  33.             
  34.             // 注意:下面的代码永远不会执行
  35.             printl("这行代码不会被执行!");
  36.         }
  37.     }
  38. }

  39. // 示例场景2:用户交互终止
  40. function example2_userInteractionStop() {
  41.     printl("=== 示例2:用户交互终止程序 ===");
  42.    
  43.     // 创建一个后台检查线程(模拟用户交互)
  44.     let checkInterval = setInterval(() => {
  45.         // 模拟检测用户是否点击了停止按钮
  46.         // 实际应用中,这里可能是检测UI元素或特定条件
  47.         let userWantsToStop = Math.random() > 0.8; // 模拟20%概率用户点击停止
  48.         
  49.         if (userWantsToStop) {
  50.             printl("&#128721; 用户请求停止程序");
  51.             
  52.             // 保存当前状态
  53.             printl("&#128190; 正在保存程序状态...");
  54.             sleep.second(1);
  55.             printl("✅ 状态保存完成");
  56.             
  57.             // 停止定时器
  58.             clearInterval(checkInterval);
  59.             
  60.             // 停止程序
  61.             selfRunTime.stop();
  62.         }
  63.     }, 2000);
  64.    
  65.     // 主程序继续运行
  66.     printl("程序正在后台运行,每2秒检查一次用户是否请求停止...");
  67.     let counter = 0;
  68.    
  69.     while (true) {
  70.         printl(`主程序执行中... ${counter++}`);
  71.         sleep.second(1);
  72.     }
  73. }

  74. // 示例场景3:异常处理与安全终止
  75. function example3_exceptionHandlingStop() {
  76.     printl("=== 示例3:异常处理与安全终止 ===");
  77.    
  78.     try {
  79.         // 执行一些操作
  80.         for (let i = 0; i < 5; i++) {
  81.             printl(`执行操作 ${i+1}/5`);
  82.             sleep.second(1);
  83.             
  84.             // 模拟异常情况
  85.             if (i === 2) {
  86.                 printl("❌ 发生严重错误!");
  87.                 throw new Error("模拟的严重系统错误");
  88.             }
  89.         }
  90.     } catch (error) {
  91.         printl(`捕获到异常: ${error.message}`);
  92.         printl("&#128274; 正在执行安全终止流程...");
  93.         
  94.         // 执行安全终止前的必要操作
  95.         printl("1. 释放资源");
  96.         printl("2. 记录错误日志");
  97.         printl("3. 通知监控系统");
  98.         sleep.second(2);
  99.         
  100.         // 安全终止程序
  101.         selfRunTime.stop();
  102.     }
  103. }

  104. /**
  105. * 方法二:runOnUIThread(在UI线程中运行函数)详解与示例
  106. * 功能:将指定函数切换到UI线程执行(适用于UI相关操作,避免线程安全问题)
  107. * 参数:需执行的函数(支持箭头函数或普通函数)
  108. * 返回值:无(Void)
  109. */

  110. // 示例场景1:基本UI更新操作
  111. function example4_basicUIUpdate() {
  112.     printl("=== 示例4:基本UI更新操作 ===");
  113.    
  114.     // 在非UI线程中
  115.     printl("在非UI线程执行任务...");
  116.    
  117.     // 模拟耗时操作
  118.     sleep.second(2);
  119.    
  120.     // 更新UI必须在UI线程中进行
  121.     selfRunTime.runOnUIThread(() => {
  122.         printl("我已经跑在UI线程里了,可以安全地更新UI组件");
  123.         
  124.         // 模拟UI更新操作
  125.         // 实际应用中,这里可能是更新文本、按钮状态、进度条等
  126.         printl("✅ UI组件更新完成");
  127.     });
  128.    
  129.     printl("非UI线程继续执行其他任务...");
  130. }

  131. // 示例场景2:复杂UI交互操作
  132. function example5_complexUIInteraction() {
  133.     printl("=== 示例5:复杂UI交互操作 ===");
  134.    
  135.     // 模拟后台数据处理
  136.     let processData = () => {
  137.         printl("&#128260; 后台正在处理大量数据...");
  138.         sleep.second(3);
  139.         return { status: "success", result: "处理完成的数据" };
  140.     };
  141.    
  142.     // 启动后台任务
  143.     printl("启动后台数据处理任务...");
  144.    
  145.     // 模拟异步操作完成后的回调
  146.     setTimeout(() => {
  147.         let dataResult = processData();
  148.         
  149.         // 当数据处理完成后,在UI线程中更新界面
  150.         selfRunTime.runOnUIThread(() => {
  151.             printl("&#128202; 在UI线程中准备更新界面");
  152.             
  153.             // 示例:更新多个UI组件
  154.             printl(`更新状态显示: ${dataResult.status}`);
  155.             printl(`更新结果文本: ${dataResult.result}`);
  156.             printl("更新进度条到100%");
  157.             printl("启用完成按钮");
  158.             printl("隐藏加载动画");
  159.             
  160.             printl("&#127881; 复杂UI交互完成");
  161.         });
  162.     }, 1000);
  163.    
  164.     printl("后台任务已启动,主线程继续执行...");
  165. }

  166. // 示例场景3:定时器与UI线程结合
  167. function example6_timerAndUIThread() {
  168.     printl("=== 示例6:定时器与UI线程结合 ===");
  169.    
  170.     let countdown = 5;
  171.     printl(`开始倒计时: ${countdown}秒`);
  172.    
  173.     // 创建定时器
  174.     let timer = setInterval(() => {
  175.         countdown--;
  176.         
  177.         // 每次倒计时更新都必须在UI线程中进行
  178.         selfRunTime.runOnUIThread(() => {
  179.             printl(`倒计时更新: ${countdown}秒`);
  180.             
  181.             // 模拟UI更新
  182.             // 实际应用中,这里可能是更新倒计时文本、进度条等
  183.             if (countdown <= 0) {
  184.                 printl("⏰ 倒计时结束!");
  185.                 clearInterval(timer);
  186.                
  187.                 // 倒计时结束后的UI操作
  188.                 printl("&#128276; 显示完成通知");
  189.                 printl("&#127919; 执行最终UI状态更新");
  190.             }
  191.         });
  192.     }, 1000);
  193. }

  194. // 示例场景4:OCR识别结果的UI展示
  195. function example7_ocrResultUI() {
  196.     printl("=== 示例7:OCR识别结果的UI展示 ===");
  197.    
  198.     // 模拟OCR识别过程
  199.     printl("&#128247; 开始屏幕截图和OCR识别...");
  200.    
  201.     // 在后台执行OCR识别(模拟)
  202.     setTimeout(() => {
  203.         // 模拟OCR识别结果
  204.         let ocrResult = {
  205.             text: "识别到的文本内容",
  206.             confidence: 0.95,
  207.             regions: [
  208.                 { text: "按钮1", x: 0.1, y: 0.2, width: 0.2, height: 0.1 },
  209.                 { text: "按钮2", x: 0.4, y: 0.2, width: 0.2, height: 0.1 }
  210.             ]
  211.         };
  212.         
  213.         printl("✅ OCR识别完成,准备在UI上展示结果");
  214.         
  215.         // 在UI线程中处理和显示OCR结果
  216.         selfRunTime.runOnUIThread(() => {
  217.             printl("&#128203; OCR识别结果UI展示开始");
  218.             
  219.             // 显示识别结果统计
  220.             printl(`识别文本: ${ocrResult.text}`);
  221.             printl(`置信度: ${(ocrResult.confidence * 100).toFixed(1)}%`);
  222.             printl(`识别区域数量: ${ocrResult.regions.length}`);
  223.             
  224.             // 遍历并展示每个识别区域
  225.             ocrResult.regions.forEach((region, index) => {
  226.                 printl(`区域${index+1}: "${region.text}" - 位置: (${region.x.toFixed(2)}, ${region.y.toFixed(2)}) 大小: ${region.width.toFixed(2)}x${region.height.toFixed(2)}`);
  227.                
  228.                 // 模拟在UI上标记识别区域
  229.                 // 实际应用中,这里可能是绘制矩形、高亮文本等
  230.                 printl(`   ✅ 在UI上标记了文本 "${region.text}"`);
  231.             });
  232.             
  233.             printl("&#127912; OCR结果UI展示完成");
  234.         });
  235.     }, 2000);
  236. }

  237. // 示例场景5:runOnUIThread与异常处理结合
  238. function example8_exceptionHandlingUIThread() {
  239.     printl("=== 示例8:runOnUIThread与异常处理结合 ===");
  240.    
  241.     // 模拟一个可能失败的UI操作
  242.     selfRunTime.runOnUIThread(() => {
  243.         try {
  244.             printl("&#128269; 在UI线程中执行可能失败的操作");
  245.             
  246.             // 模拟UI操作
  247.             printl("&#128221; 更新用户信息");
  248.             printl("&#128260; 刷新数据列表");
  249.             
  250.             // 模拟UI操作中可能发生的异常
  251.             let shouldThrowError = Math.random() > 0.5; // 50%概率抛出异常
  252.             if (shouldThrowError) {
  253.                 printl("❌ UI操作发生异常");
  254.                 throw new Error("UI更新失败:组件未找到");
  255.             }
  256.             
  257.             printl("✅ UI操作成功完成");
  258.         } catch (error) {
  259.             // 在UI线程中捕获和处理异常
  260.             printl(`⚠️ UI线程中捕获到异常: ${error.message}`);
  261.             printl("&#128260; 执行UI异常恢复策略");
  262.             
  263.             // 模拟异常恢复操作
  264.             printl("1. 显示错误提示给用户");
  265.             printl("2. 恢复UI到安全状态");
  266.             printl("3. 记录异常日志");
  267.         }
  268.     });
  269. }

  270. /**
  271. * 综合示例:结合stop和runOnUIThread方法的完整应用场景
  272. */
  273. function comprehensiveExample() {
  274.     printl("=== 综合示例:自动化任务管理系统 ===");
  275.     printl("启动自动化任务...");
  276.    
  277.     // 任务状态
  278.     let taskStatus = {
  279.         completed: 0,
  280.         total: 10,
  281.         isRunning: true,
  282.         lastError: null
  283.     };
  284.    
  285.     // 更新UI显示任务状态
  286.     function updateTaskUI() {
  287.         selfRunTime.runOnUIThread(() => {
  288.             printl(`&#128202; 任务进度: ${taskStatus.completed}/${taskStatus.total} (${Math.round((taskStatus.completed/taskStatus.total)*100)}%)`);
  289.             
  290.             // 模拟UI更新
  291.             printl(`   ✅ 更新进度条到 ${Math.round((taskStatus.completed/taskStatus.total)*100)}%`);
  292.             
  293.             if (taskStatus.lastError) {
  294.                 printl(`   ❌ 最后错误: ${taskStatus.lastError}`);
  295.                 printl(`   ⚠️  显示错误提示给用户`);
  296.             }
  297.             
  298.             if (!taskStatus.isRunning) {
  299.                 printl(`   &#127881; 任务已完成或停止`);
  300.                 printl(`   &#128221; 更新最终状态显示`);
  301.             }
  302.         });
  303.     }
  304.    
  305.     // 检查任务终止条件
  306.     function checkTerminationConditions() {
  307.         // 检查是否达到最大错误次数
  308.         let maxErrors = 3;
  309.         let errorCount = taskStatus.lastError ? 1 : 0; // 简化示例,实际应用可能更复杂
  310.         
  311.         if (errorCount >= maxErrors) {
  312.             printl("❌ 达到最大错误次数,准备停止任务");
  313.             taskStatus.isRunning = false;
  314.             
  315.             selfRunTime.runOnUIThread(() => {
  316.                 printl("&#128308; 显示任务失败通知");
  317.                 printl("&#128190; 保存当前任务状态");
  318.             });
  319.             
  320.             sleep.second(1);
  321.             printl("&#128721; 任务停止");
  322.             selfRunTime.stop();
  323.         }
  324.     }
  325.    
  326.     // 模拟任务执行
  327.     function executeTask() {
  328.         if (!taskStatus.isRunning) return;
  329.         
  330.         try {
  331.             printl(`&#128260; 执行任务 ${taskStatus.completed + 1}/${taskStatus.total}`);
  332.             
  333.             // 模拟任务执行
  334.             sleep.second(1);
  335.             
  336.             // 模拟随机错误
  337.             let shouldError = Math.random() > 0.8; // 20%概率出错
  338.             if (shouldError) {
  339.                 throw new Error(`任务 ${taskStatus.completed + 1} 执行失败`);
  340.             }
  341.             
  342.             // 任务成功
  343.             taskStatus.completed++;
  344.             taskStatus.lastError = null;
  345.             printl(`✅ 任务 ${taskStatus.completed} 执行成功`);
  346.             
  347.             // 更新UI
  348.             updateTaskUI();
  349.             
  350.             // 检查是否完成所有任务
  351.             if (taskStatus.completed >= taskStatus.total) {
  352.                 printl("&#127881; 所有任务执行完成!");
  353.                 taskStatus.isRunning = false;
  354.                
  355.                 selfRunTime.runOnUIThread(() => {
  356.                     printl("✅ 显示任务完成通知");
  357.                     printl("&#127942; 更新最终进度到100%");
  358.                 });
  359.                
  360.                 sleep.second(1);
  361.                 printl("&#128721; 程序正常结束");
  362.                 selfRunTime.stop();
  363.             } else {
  364.                 // 继续下一个任务
  365.                 executeTask();
  366.             }
  367.             
  368.         } catch (error) {
  369.             printl(`❌ 任务执行出错: ${error.message}`);
  370.             taskStatus.lastError = error.message;
  371.             
  372.             // 更新UI显示错误
  373.             updateTaskUI();
  374.             
  375.             // 检查终止条件
  376.             checkTerminationConditions();
  377.             
  378.             // 如果未达到终止条件,继续执行
  379.             if (taskStatus.isRunning) {
  380.                 printl("&#128260; 准备重试任务...");
  381.                 sleep.second(1);
  382.                 executeTask();
  383.             }
  384.         }
  385.     }
  386.    
  387.     // 启动任务
  388.     updateTaskUI();
  389.     executeTask();
  390. }

  391. /**
  392. * 运行示例选择
  393. * 取消注释下面要运行的示例函数调用来测试不同场景
  394. */

  395. // 运行stop方法示例
  396. // example1_conditionBasedStop();  // 条件检测与程序终止
  397. // example2_userInteractionStop(); // 用户交互终止
  398. // example3_exceptionHandlingStop(); // 异常处理与安全终止

  399. // 运行runOnUIThread方法示例
  400. // example4_basicUIUpdate(); // 基本UI更新操作
  401. // example5_complexUIInteraction(); // 复杂UI交互操作
  402. // example6_timerAndUIThread(); // 定时器与UI线程结合
  403. // example7_ocrResultUI(); // OCR识别结果的UI展示
  404. // example8_exceptionHandlingUIThread(); // 异常处理与UI线程结合

  405. // 运行综合示例
  406. comprehensiveExample(); // 结合stop和runOnUIThread的综合应用

  407. printl("\n&#128218; 示例代码运行完成。请取消注释相应函数调用以测试不同场景。");
复制代码
[color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法一:stop 停止运行
项目
说明
功能
停止运行、停止脚本
方法声明
Void stop()
返回值
Void
参数
案例
selfRunTime.stop()
[color=var(--md-box-h3-text-color,var(--md-box-global-text-color))]方法二:runOnUIThread ui 线程中运行函数
项目
说明
功能
ui 线程中运行函数
方法声明
Void runOnUIThread(String function)
返回值
Void
参数
String function: 函数
案例
selfRunTime.runOnUIThread( ()=>{
   printl('我已经跑在ui线程里了')   
})



  1. // 方法一:stop(停止运行)
  2. // 功能:停止当前运行的程序或脚本
  3. // 参数:无
  4. // 返回值:无(Void)
  5. // 测试示例:
  6. selfRunTime.stop();

  7. // 方法二:runOnUIThread(在UI线程中运行函数)
  8. // 功能:将指定函数切换到UI线程执行(适用于UI相关操作,避免线程安全问题)
  9. // 参数:需执行的函数(支持箭头函数或普通函数)
  10. // 返回值:无(Void)
  11. // 测试示例:
  12. selfRunTime.runOnUIThread( ()=>{
  13.    printl('我已经跑在ui线程里了')   
  14. });
复制代码








欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) Powered by Discuz! X3.2