claude-3.5-sonnet写的一段代码,大家看看为何没有数据刷新?

mndsoft 9月前 651

让claude-3.5-sonnet 写一段 使用工作线程动态刷新 listview中的数据,代码写的可以运行,但是没有看到listview数据进行刷新,不知道咋给claude-3.5-sonnet 说了,,近期在学习线程技术,哈哈,看来还得高手(人类)来修改。



Code AardioLine:106复制
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
    • import win.ui;
    • import time;
    • import thread;
    • /*DSG{{*/
    • var winform = win.form(text="ListView 动态刷新示例";right=759;bottom=469)
    • winform.add(
    • listview={cls="listview";left=10;top=10;right=749;bottom=459;edge=1;fullRow=1;gridLines=1;z=1}
    • )
    • /*}}*/
    • // 设置ListView列头
    • winform.listview.insertColumn("ID", 50);
    • winform.listview.insertColumn("名称", 100);
    • winform.listview.insertColumn("值", 100);
    • winform.listview.insertColumn("变化", 100);
    • winform.listview.insertColumn("时间戳", 200);
    • // 扩展数据源
    • var dataSource = {
    • {id=1; name="温度"; unit="°C"};
    • {id=2; name="湿度"; unit="%"};
    • {id=3; name="压力"; unit="hPa"};
    • {id=4; name="风速"; unit="m/s"};
    • {id=5; name="光照"; unit="lux"};
    • {id=6; name="噪音"; unit="dB"};
    • {id=7; name="PM2.5"; unit="μg/m³"};
    • {id=8; name="CO2"; unit="ppm"};
    • {id=9; name="电压"; unit="V"};
    • {id=10; name="电流"; unit="A"};
    • }
    • // 更新数据的函数(在后台线程中运行)
    • updateData = function() {
    • var lastValues = {};
    • while(!thread.get("stopThread")) {
    • var newData = {};
    • for(i=1; #dataSource; 1) {
    • var item = table.clone(dataSource[i]);
    • item.value = math.round(math.random() * 100, 2); // 生成0-100之间的随机值,保留两位小数
    • item.timestamp = tostring(..time(,"%Y-%m-%d %H:%M:%S"));
    • // 计算变化
    • if(lastValues[item.id]) {
    • item.change = math.round(item.value - lastValues[item.id], 2);
    • } else {
    • item.change = 0;
    • }
    • lastValues[item.id] = item.value;
    • table.push(newData, item);
    • }
    • thread.set("newData", newData);
    • thread.set("dataReady", true);
    • thread.sleep(1000); // 延迟1秒
    • }
    • }
    • // 更新ListView的函数(在主线程中运行)
    • updateListView = function() {
    • var newData = thread.get("newData");
    • if(newData) {
    • for(i=1; #newData; 1) {
    • var item = newData[i];
    • var changeStr = item.change > 0 ? "+" + tostring(item.change) : tostring(item.change);
    • var changeColor = item.change > 0 ? 0xFF0000 : (item.change < 0 ? 0x0000FF : 0x000000);
    • // 更新现有行或添加新行
    • if(i <= winform.listview.count) {
    • winform.listview.setItem(i, 1, tostring(item.id));
    • winform.listview.setItem(i, 2, item.name);
    • winform.listview.setItem(i, 3, tostring(item.value) + " " + item.unit);
    • winform.listview.setItem(i, 4, changeStr);
    • winform.listview.setItem(i, 5, item.timestamp);
    • } else {
    • winform.listview.addItem({
    • tostring(item.id);
    • item.name;
    • tostring(item.value) + " " + item.unit;
    • changeStr;
    • item.timestamp;
    • });
    • }
    • winform.listview.setItemTextColor(i, 4, changeColor);
    • }
    • thread.set("dataReady", false);
    • }
    • }
    • // 创建后台线程
    • var dataThread = thread.create(updateData);
    • // 在主线程中设置定时器来更新UI
    • winform.setInterval(500, function() {
    • if(thread.get("dataReady")) {
    • updateListView();
    • }
    • });
    • winform.onClose = function(hwnd,message,wParam,lParam){
    • thread.set("stopThread", true); // 设置停止标志
    • thread.waitClose(dataThread); // 等待线程结束
    • }
    • winform.show();
    • win.loopMessage();


    最新回复 (3)
    • 光庆 9月前
      0 2
      我觉得,AI 目前能写的,可能以片段为主。 代码量稍微大一点的,可能逻辑性、连贯性就欠缺,不一定能正常运行。 不过可以参考其思路,再手动修改。
    • 光庆 9月前
      0 3
      Code AardioLine:106复制
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
    • 32.
    • 33.
    • 34.
    • 35.
    • 36.
    • 37.
    • 38.
    • 39.
    • 40.
    • 41.
    • 42.
    • 43.
    • 44.
    • 45.
    • 46.
    • 47.
    • 48.
    • 49.
    • 50.
    • 51.
    • 52.
    • 53.
    • 54.
    • 55.
    • 56.
    • 57.
    • 58.
    • 59.
    • 60.
    • 61.
    • 62.
    • 63.
    • 64.
    • 65.
    • 66.
    • 67.
    • 68.
    • 69.
    • 70.
    • 71.
    • 72.
    • 73.
    • 74.
    • 75.
    • 76.
    • 77.
    • 78.
    • 79.
    • 80.
    • 81.
    • 82.
    • 83.
    • 84.
    • 85.
    • 86.
    • 87.
    • 88.
    • 89.
    • 90.
    • 91.
    • 92.
    • 93.
    • 94.
    • 95.
    • 96.
    • 97.
    • 98.
    • 99.
    • 100.
    • 101.
    • 102.
    • 103.
    • 104.
    • 105.
    • 106.
      • import win.ui;
      • import time;
      • import thread;
      • /*DSG{{*/
      • var winform = win.form(text="ListView 动态刷新示例";right=759;bottom=469)
      • winform.add(
      • listview={cls="listview";left=10;top=10;right=749;bottom=459;edge=1;fullRow=1;gridLines=1;z=1}
      • )
      • /*}}*/
      • // 设置ListView列头
      • winform.listview.insertColumn("ID", 50);
      • winform.listview.insertColumn("名称", 100);
      • winform.listview.insertColumn("值", 100);
      • winform.listview.insertColumn("变化", 100);
      • winform.listview.insertColumn("时间戳", 200);
      • // 扩展数据源
      • var dataSource = {
      • {id=1; name="温度"; unit="°C"};
      • {id=2; name="湿度"; unit="%"};
      • {id=3; name="压力"; unit="hPa"};
      • {id=4; name="风速"; unit="m/s"};
      • {id=5; name="光照"; unit="lux"};
      • {id=6; name="噪音"; unit="dB"};
      • {id=7; name="PM2.5"; unit="μg/m³"};
      • {id=8; name="CO2"; unit="ppm"};
      • {id=9; name="电压"; unit="V"};
      • {id=10; name="电流"; unit="A"};
      • }
      • // 更新数据的函数(在后台线程中运行)
      • updateData = function(dataSource) {
      • var lastValues = {};
      • while(!thread.get("stopThread")) {
      • var newData = {};
      • for(i=1; #dataSource; 1) {
      • var item = table.clone(dataSource[i]);
      • item.value = math.round(math.random() * 100, 2); // 生成0-100之间的随机值,保留两位小数
      • item.timestamp = tostring(..time(,"%Y-%m-%d %H:%M:%S"));
      • // 计算变化
      • if(lastValues[item.id]) {
      • item.change = math.round(item.value - lastValues[item.id], 2);
      • } else {
      • item.change = 0;
      • }
      • lastValues[item.id] = item.value;
      • table.push(newData, item);
      • }
      • thread.set("newData", newData);
      • thread.set("dataReady", true);
      • thread.sleep(1000); // 延迟1秒
      • }
      • }
      • // 更新ListView的函数(在主线程中运行)
      • updateListView = function() {
      • var newData = thread.get("newData");
      • if(newData) {
      • for(i=1; #newData; 1) {
      • var item = newData[i];
      • var changeStr = item.change > 0 ? "+" + tostring(item.change) : tostring(item.change);
      • var changeColor = item.change > 0 ? 0xFF0000 : (item.change < 0 ? 0x0000FF : 0x000000);
      • // 更新现有行或添加新行
      • if(i <= winform.listview.count) {
      • winform.listview.setItem(i, 1, tostring(item.id));
      • winform.listview.setItem(i, 2, item.name);
      • winform.listview.setItem(i, 3, tostring(item.value) + " " + item.unit);
      • winform.listview.setItem(i, 4, changeStr);
      • winform.listview.setItem(i, 5, item.timestamp);
      • } else {
      • winform.listview.addItem({
      • tostring(item.id);
      • item.name;
      • tostring(item.value) + " " + item.unit;
      • changeStr;
      • item.timestamp;
      • });
      • }
      • //winform.listview.setItemTextColor(i, 4, changeColor);
      • }
      • thread.set("dataReady", false);
      • }
      • }
      • // 创建后台线程
      • var dataThread = thread.create(updateData,dataSource);
      • // 在主线程中设置定时器来更新UI
      • winform.setInterval(500, function() {
      • if(thread.get("dataReady")) {
      • updateListView();
      • }
      • });
      • winform.onClose = function(hwnd,message,wParam,lParam){
      • thread.set("stopThread", true); // 设置停止标志
      • thread.waitClose(dataThread); // 等待线程结束
      • }
      • winform.show();
      • win.loopMessage();


    • mndsoft 9月前
      0 4
      厉害,大师堪比AI,但是好像定时器没有起作用,没看到随机数据刷新。
    返回