|
|
苹果JS代码运行时[selfRunTime]小结
- // 方法一:stop(停止运行)
- // 功能:停止当前运行的程序或脚本
- // 参数:无
- // 返回值:无(Void)
- // 测试示例:
- selfRunTime.stop();
- // 方法二:runOnUIThread(在UI线程中运行函数)
- // 功能:将指定函数切换到UI线程执行(适用于UI相关操作,避免线程安全问题)
- // 参数:需执行的函数(支持箭头函数或普通函数)
- // 返回值:无(Void)
- // 测试示例:
- selfRunTime.runOnUIThread( ()=>{
- printl('我已经跑在ui线程里了')
- });
复制代码 [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线程里了') }) |
- /**
- * AIWROK 软件苹果开发文档 - stop 和 runOnUIThread 方法详解
- * AIWROK软件安卓交流QQ群711841924
- * 苹果内测软件QQ群648461709
- */
- /**
- * 方法一:stop(停止运行)详解与示例
- * 功能:停止当前运行的程序或脚本
- * 参数:无
- * 返回值:无(Void)
- */
- // 示例场景1:条件检测与程序终止
- function example1_conditionBasedStop() {
- printl("=== 示例1:条件检测与程序终止 ===");
-
- // 模拟一个循环任务
- for (let i = 0; i < 100; i++) {
- printl(`执行任务 ${i}/100...`);
-
- // 模拟一些处理时间
- sleep.second(0.5);
-
- // 检测终止条件
- if (i === 10) {
- printl("⚠️ 检测到异常条件,准备终止程序...");
- // 执行清理工作
- printl("🔄 正在执行清理操作...");
- sleep.second(1);
- printl("✅ 清理完成,程序终止");
-
- // 停止程序运行
- selfRunTime.stop();
-
- // 注意:下面的代码永远不会执行
- printl("这行代码不会被执行!");
- }
- }
- }
- // 示例场景2:用户交互终止
- function example2_userInteractionStop() {
- printl("=== 示例2:用户交互终止程序 ===");
-
- // 创建一个后台检查线程(模拟用户交互)
- let checkInterval = setInterval(() => {
- // 模拟检测用户是否点击了停止按钮
- // 实际应用中,这里可能是检测UI元素或特定条件
- let userWantsToStop = Math.random() > 0.8; // 模拟20%概率用户点击停止
-
- if (userWantsToStop) {
- printl("🛑 用户请求停止程序");
-
- // 保存当前状态
- printl("💾 正在保存程序状态...");
- sleep.second(1);
- printl("✅ 状态保存完成");
-
- // 停止定时器
- clearInterval(checkInterval);
-
- // 停止程序
- selfRunTime.stop();
- }
- }, 2000);
-
- // 主程序继续运行
- printl("程序正在后台运行,每2秒检查一次用户是否请求停止...");
- let counter = 0;
-
- while (true) {
- printl(`主程序执行中... ${counter++}`);
- sleep.second(1);
- }
- }
- // 示例场景3:异常处理与安全终止
- function example3_exceptionHandlingStop() {
- printl("=== 示例3:异常处理与安全终止 ===");
-
- try {
- // 执行一些操作
- for (let i = 0; i < 5; i++) {
- printl(`执行操作 ${i+1}/5`);
- sleep.second(1);
-
- // 模拟异常情况
- if (i === 2) {
- printl("❌ 发生严重错误!");
- throw new Error("模拟的严重系统错误");
- }
- }
- } catch (error) {
- printl(`捕获到异常: ${error.message}`);
- printl("🔒 正在执行安全终止流程...");
-
- // 执行安全终止前的必要操作
- printl("1. 释放资源");
- printl("2. 记录错误日志");
- printl("3. 通知监控系统");
- sleep.second(2);
-
- // 安全终止程序
- selfRunTime.stop();
- }
- }
- /**
- * 方法二:runOnUIThread(在UI线程中运行函数)详解与示例
- * 功能:将指定函数切换到UI线程执行(适用于UI相关操作,避免线程安全问题)
- * 参数:需执行的函数(支持箭头函数或普通函数)
- * 返回值:无(Void)
- */
- // 示例场景1:基本UI更新操作
- function example4_basicUIUpdate() {
- printl("=== 示例4:基本UI更新操作 ===");
-
- // 在非UI线程中
- printl("在非UI线程执行任务...");
-
- // 模拟耗时操作
- sleep.second(2);
-
- // 更新UI必须在UI线程中进行
- selfRunTime.runOnUIThread(() => {
- printl("我已经跑在UI线程里了,可以安全地更新UI组件");
-
- // 模拟UI更新操作
- // 实际应用中,这里可能是更新文本、按钮状态、进度条等
- printl("✅ UI组件更新完成");
- });
-
- printl("非UI线程继续执行其他任务...");
- }
- // 示例场景2:复杂UI交互操作
- function example5_complexUIInteraction() {
- printl("=== 示例5:复杂UI交互操作 ===");
-
- // 模拟后台数据处理
- let processData = () => {
- printl("🔄 后台正在处理大量数据...");
- sleep.second(3);
- return { status: "success", result: "处理完成的数据" };
- };
-
- // 启动后台任务
- printl("启动后台数据处理任务...");
-
- // 模拟异步操作完成后的回调
- setTimeout(() => {
- let dataResult = processData();
-
- // 当数据处理完成后,在UI线程中更新界面
- selfRunTime.runOnUIThread(() => {
- printl("📊 在UI线程中准备更新界面");
-
- // 示例:更新多个UI组件
- printl(`更新状态显示: ${dataResult.status}`);
- printl(`更新结果文本: ${dataResult.result}`);
- printl("更新进度条到100%");
- printl("启用完成按钮");
- printl("隐藏加载动画");
-
- printl("🎉 复杂UI交互完成");
- });
- }, 1000);
-
- printl("后台任务已启动,主线程继续执行...");
- }
- // 示例场景3:定时器与UI线程结合
- function example6_timerAndUIThread() {
- printl("=== 示例6:定时器与UI线程结合 ===");
-
- let countdown = 5;
- printl(`开始倒计时: ${countdown}秒`);
-
- // 创建定时器
- let timer = setInterval(() => {
- countdown--;
-
- // 每次倒计时更新都必须在UI线程中进行
- selfRunTime.runOnUIThread(() => {
- printl(`倒计时更新: ${countdown}秒`);
-
- // 模拟UI更新
- // 实际应用中,这里可能是更新倒计时文本、进度条等
- if (countdown <= 0) {
- printl("⏰ 倒计时结束!");
- clearInterval(timer);
-
- // 倒计时结束后的UI操作
- printl("🔔 显示完成通知");
- printl("🎯 执行最终UI状态更新");
- }
- });
- }, 1000);
- }
- // 示例场景4:OCR识别结果的UI展示
- function example7_ocrResultUI() {
- printl("=== 示例7:OCR识别结果的UI展示 ===");
-
- // 模拟OCR识别过程
- printl("📷 开始屏幕截图和OCR识别...");
-
- // 在后台执行OCR识别(模拟)
- setTimeout(() => {
- // 模拟OCR识别结果
- let ocrResult = {
- text: "识别到的文本内容",
- confidence: 0.95,
- regions: [
- { text: "按钮1", x: 0.1, y: 0.2, width: 0.2, height: 0.1 },
- { text: "按钮2", x: 0.4, y: 0.2, width: 0.2, height: 0.1 }
- ]
- };
-
- printl("✅ OCR识别完成,准备在UI上展示结果");
-
- // 在UI线程中处理和显示OCR结果
- selfRunTime.runOnUIThread(() => {
- printl("📋 OCR识别结果UI展示开始");
-
- // 显示识别结果统计
- printl(`识别文本: ${ocrResult.text}`);
- printl(`置信度: ${(ocrResult.confidence * 100).toFixed(1)}%`);
- printl(`识别区域数量: ${ocrResult.regions.length}`);
-
- // 遍历并展示每个识别区域
- ocrResult.regions.forEach((region, index) => {
- printl(`区域${index+1}: "${region.text}" - 位置: (${region.x.toFixed(2)}, ${region.y.toFixed(2)}) 大小: ${region.width.toFixed(2)}x${region.height.toFixed(2)}`);
-
- // 模拟在UI上标记识别区域
- // 实际应用中,这里可能是绘制矩形、高亮文本等
- printl(` ✅ 在UI上标记了文本 "${region.text}"`);
- });
-
- printl("🎨 OCR结果UI展示完成");
- });
- }, 2000);
- }
- // 示例场景5:runOnUIThread与异常处理结合
- function example8_exceptionHandlingUIThread() {
- printl("=== 示例8:runOnUIThread与异常处理结合 ===");
-
- // 模拟一个可能失败的UI操作
- selfRunTime.runOnUIThread(() => {
- try {
- printl("🔍 在UI线程中执行可能失败的操作");
-
- // 模拟UI操作
- printl("📝 更新用户信息");
- printl("🔄 刷新数据列表");
-
- // 模拟UI操作中可能发生的异常
- let shouldThrowError = Math.random() > 0.5; // 50%概率抛出异常
- if (shouldThrowError) {
- printl("❌ UI操作发生异常");
- throw new Error("UI更新失败:组件未找到");
- }
-
- printl("✅ UI操作成功完成");
- } catch (error) {
- // 在UI线程中捕获和处理异常
- printl(`⚠️ UI线程中捕获到异常: ${error.message}`);
- printl("🔄 执行UI异常恢复策略");
-
- // 模拟异常恢复操作
- printl("1. 显示错误提示给用户");
- printl("2. 恢复UI到安全状态");
- printl("3. 记录异常日志");
- }
- });
- }
- /**
- * 综合示例:结合stop和runOnUIThread方法的完整应用场景
- */
- function comprehensiveExample() {
- printl("=== 综合示例:自动化任务管理系统 ===");
- printl("启动自动化任务...");
-
- // 任务状态
- let taskStatus = {
- completed: 0,
- total: 10,
- isRunning: true,
- lastError: null
- };
-
- // 更新UI显示任务状态
- function updateTaskUI() {
- selfRunTime.runOnUIThread(() => {
- printl(`📊 任务进度: ${taskStatus.completed}/${taskStatus.total} (${Math.round((taskStatus.completed/taskStatus.total)*100)}%)`);
-
- // 模拟UI更新
- printl(` ✅ 更新进度条到 ${Math.round((taskStatus.completed/taskStatus.total)*100)}%`);
-
- if (taskStatus.lastError) {
- printl(` ❌ 最后错误: ${taskStatus.lastError}`);
- printl(` ⚠️ 显示错误提示给用户`);
- }
-
- if (!taskStatus.isRunning) {
- printl(` 🎉 任务已完成或停止`);
- printl(` 📝 更新最终状态显示`);
- }
- });
- }
-
- // 检查任务终止条件
- function checkTerminationConditions() {
- // 检查是否达到最大错误次数
- let maxErrors = 3;
- let errorCount = taskStatus.lastError ? 1 : 0; // 简化示例,实际应用可能更复杂
-
- if (errorCount >= maxErrors) {
- printl("❌ 达到最大错误次数,准备停止任务");
- taskStatus.isRunning = false;
-
- selfRunTime.runOnUIThread(() => {
- printl("🔴 显示任务失败通知");
- printl("💾 保存当前任务状态");
- });
-
- sleep.second(1);
- printl("🛑 任务停止");
- selfRunTime.stop();
- }
- }
-
- // 模拟任务执行
- function executeTask() {
- if (!taskStatus.isRunning) return;
-
- try {
- printl(`🔄 执行任务 ${taskStatus.completed + 1}/${taskStatus.total}`);
-
- // 模拟任务执行
- sleep.second(1);
-
- // 模拟随机错误
- let shouldError = Math.random() > 0.8; // 20%概率出错
- if (shouldError) {
- throw new Error(`任务 ${taskStatus.completed + 1} 执行失败`);
- }
-
- // 任务成功
- taskStatus.completed++;
- taskStatus.lastError = null;
- printl(`✅ 任务 ${taskStatus.completed} 执行成功`);
-
- // 更新UI
- updateTaskUI();
-
- // 检查是否完成所有任务
- if (taskStatus.completed >= taskStatus.total) {
- printl("🎉 所有任务执行完成!");
- taskStatus.isRunning = false;
-
- selfRunTime.runOnUIThread(() => {
- printl("✅ 显示任务完成通知");
- printl("🏆 更新最终进度到100%");
- });
-
- sleep.second(1);
- printl("🛑 程序正常结束");
- selfRunTime.stop();
- } else {
- // 继续下一个任务
- executeTask();
- }
-
- } catch (error) {
- printl(`❌ 任务执行出错: ${error.message}`);
- taskStatus.lastError = error.message;
-
- // 更新UI显示错误
- updateTaskUI();
-
- // 检查终止条件
- checkTerminationConditions();
-
- // 如果未达到终止条件,继续执行
- if (taskStatus.isRunning) {
- printl("🔄 准备重试任务...");
- sleep.second(1);
- executeTask();
- }
- }
- }
-
- // 启动任务
- updateTaskUI();
- executeTask();
- }
- /**
- * 运行示例选择
- * 取消注释下面要运行的示例函数调用来测试不同场景
- */
- // 运行stop方法示例
- // example1_conditionBasedStop(); // 条件检测与程序终止
- // example2_userInteractionStop(); // 用户交互终止
- // example3_exceptionHandlingStop(); // 异常处理与安全终止
- // 运行runOnUIThread方法示例
- // example4_basicUIUpdate(); // 基本UI更新操作
- // example5_complexUIInteraction(); // 复杂UI交互操作
- // example6_timerAndUIThread(); // 定时器与UI线程结合
- // example7_ocrResultUI(); // OCR识别结果的UI展示
- // example8_exceptionHandlingUIThread(); // 异常处理与UI线程结合
- // 运行综合示例
- comprehensiveExample(); // 结合stop和runOnUIThread的综合应用
- printl("\n📚 示例代码运行完成。请取消注释相应函数调用以测试不同场景。");
复制代码
|
|