多线程同步读写数据示例

光庆 4月前 453

原理:

1、创建一个 raw.struct 结构体数据 data

2、将 data 传递给工作线程,工作线程中可以对该数据进行修改。

3、界面线程中读取 data 数据,此数据便是工作线程中修改后的数据。

本示例用到风行者封装的 hardware.monitor 库。如果无法运行,参考代码流程,理解实现原理即可。

import win.ui;
import godking.vlistEx;
import win.ui.simpleWindow
import sys
import inet.adapter
import hardware.monitor
/*DSG{{*/
var winform = win.form(text="本机硬件监控";right=625;bottom=358;border="none";max=false;min=false;topmost=1)
winform.add(
bkplus={cls="bkplus";left=-7;top=0;right=628;bottom=29;bgcolor=16734080;z=1};
plus={cls="plus";text="计算机硬件监控";left=6;top=4;right=160;bottom=24;color=16777215;font=LOGFONT(h=-14);z=3};
vlist={cls="vlistEx";left=-2;top=28;right=626;bottom=358;edge=1;z=2}
)
/*}}*/

winform.enableDpiScaling(false);
win.ui.simpleWindow(winform);

winform.vlist.headerFont = ::LOGFONT(name="黑体";h=17;color=0xFFFFFF );/*【属性】表头文本字体*/
winform.vlist.headerAlign = 1; //表头字体居中
winform.vlist.headerBkcolor = 0xFF901E; //表头背景颜色
winform.vlist.bkColor2 = 0x98FB98;
winform.vlist.autoRedraw = false;

var IP,MAC地址;
for adptInfo in inet.adapter.each() {
    var ip = adptInfo.ipAddressList.ipAddress;
    var mac = adptInfo.mac;
    //不是0.开头的为正常网卡,有些是虚似网卡
    if !string.startWith(ip, "0.") {
        IP = ip;
        MAC地址 = mac;
        break;
    }
}
var t = {
	{"[@rowindex]","计算机名称",sys.getComputerName()}
	{"[@rowindex]","IP",IP}
	{"[@rowindex]","MAC地址",MAC地址}
	{"[@rowindex]","CPU总使用率",""}
	{"[@rowindex]","CPU平均温度",""}
	{"[@rowindex]","硬盘温度",""}
	{"[@rowindex]","剩余显存",""}
	{"[@rowindex]","内存使用率",""}
	{"[@rowindex]","上传速度",""}
	{"[@rowindex]","下载速度",""}
}
winform.vlist.setTable(t,{"序号","名称","数据"},{80,250,250},1);
winform.vlist.fillParent();

import raw.struct
var data = ..raw.struct({
	float CPUTotal;
	float CoreAverage;
	float Temperature;
	float GPUMemoryFree;
	float Memory;
	float UploadSpeed;
	float DownloadSpeed;
})

thread.invoke( 
	function(data){
		import dotNet;
		import hardware.monitor;		
		var hm = hardware.monitor();
		hm.Open();
		hm.IsCpuEnabled = true;
		hm.IsGpuEnabled = true;
		hm.IsMemoryEnabled = true;
		hm.IsStorageEnabled = true;
		hm.IsNetworkEnabled = true;
		var hh = hm.Hardware;
		do{
        	for (i = 1; hh.Count; 1) {
            	hh[i].Update();
            	for (j = 1; hh[i].Sensors.Length; 1) {
                	var sensor = hh[i].Sensors[j];
                	var sname = ..string.replace(sensor.Name," ","");
                	if data[sname]!==null {
                		data[sname] = sensor.Value:0;
                	}
            	}
       		}
       		thread.delay(1000);
		}while(true);
	},data
)

import fsys
winform.setInterval( 
	function(){
		winform.vlist.setCellText(4,3,string.format("%.1f %%",data.CPUTotal));
		winform.vlist.setCellText(5,3,string.format("%.1f °C",data.CoreAverage));
		winform.vlist.setCellText(6,3,string.format("%.1f °C",data.Temperature));
		winform.vlist.setCellText(7,3,data.GPUMemoryFree + "MB");
		winform.vlist.setCellText(8,3,string.format("%.1f %%",data.Memory));
		winform.vlist.setCellText(9,3,fsys.formatSize(data.UploadSpeed)+"/s");
		winform.vlist.setCellText(10,3,fsys.formatSize(data.DownloadSpeed)+"/s");
		winform.vlist.redraw(true);
	},1000
)

winform.show();
win.loopMessage();

另一种更新虚表数据的方式(以下代码需更新虚表库到最新版):

import win.ui;
import godking.vlistEx;
import win.ui.simpleWindow
import sys
import inet.adapter
import hardware.monitor
/*DSG{{*/
var winform = win.form(text="本机硬件监控";right=625;bottom=358;border="none";max=false;min=false;topmost=1)
winform.add(
bkplus={cls="bkplus";left=-7;top=0;right=628;bottom=29;bgcolor=16734080;z=1};
plus={cls="plus";text="计算机硬件监控";left=6;top=4;right=160;bottom=24;color=16777215;font=LOGFONT(h=-14);z=3};
vlist={cls="vlistEx";left=-2;top=28;right=626;bottom=358;edge=1;z=2}
)
/*}}*/

winform.enableDpiScaling(false);
win.ui.simpleWindow(winform);

winform.vlist.headerFont = ::LOGFONT(name="黑体";h=17;color=0xFFFFFF );/*【属性】表头文本字体*/
winform.vlist.headerAlign = 1; //表头字体居中
winform.vlist.headerBkcolor = 0xFF901E; //表头背景颜色
winform.vlist.bkColor2 = 0x98FB98;

var IP,MAC地址;
for adptInfo in inet.adapter.each() {
    var ip = adptInfo.ipAddressList.ipAddress;
    var mac = adptInfo.mac;
    //不是0.开头的为正常网卡,有些是虚似网卡
    if !string.startWith(ip, "0.") {
        IP = ip;
        MAC地址 = mac;
        break;
    }
}
var t = {
    {"[@rowindex]","计算机名称",sys.getComputerName()}
    {"[@rowindex]","IP",IP}
    {"[@rowindex]","MAC地址",MAC地址}
    {"[@rowindex]","CPU总使用率",""}
    {"[@rowindex]","CPU平均温度",""}
    {"[@rowindex]","硬盘温度",""}
    {"[@rowindex]","剩余显存",""}
    {"[@rowindex]","内存使用率",""}
    {"[@rowindex]","上传速度",""}
    {"[@rowindex]","下载速度",""}
}
winform.vlist.setTable(t,{"序号","名称","数据"},{80,250,250},1);
winform.vlist.fillParent();

import raw.struct
var data = ..raw.struct({
    float CPUTotal;
    float CoreAverage;
    float Temperature;
    float GPUMemoryFree;
    float Memory;
    float UploadSpeed;
    float DownloadSpeed;
})

thread.invoke( 
    function(data){
        import dotNet;
        import hardware.monitor;        
        var hm = hardware.monitor();
        hm.Open();
        hm.IsCpuEnabled = true;
        hm.IsGpuEnabled = true;
        hm.IsMemoryEnabled = true;
        hm.IsStorageEnabled = true;
        hm.IsNetworkEnabled = true;
        var hh = hm.Hardware;
        do{
            for (i = 1; hh.Count; 1) {
                hh[i].Update();
                for (j = 1; hh[i].Sensors.Length; 1) {
                    var sensor = hh[i].Sensors[j];
                    var sname = ..string.replace(sensor.Name," ","");
                    if data[sname]!==null {
                        data[sname] = sensor.Value:0;
                    }
                }
               }
               thread.delay(1000);
        }while(true);
    },data
)

import fsys
winform.setInterval( 
    function(){ // 批量更新虚表部分区域内容,中间没有频繁的重绘过程:
        winform.vlist.setRangeArray(4,3,{
        	{ string.format("%.1f %%",data.CPUTotal) }
        	{ string.format("%.1f °C",data.CoreAverage) }
        	{ string.format("%.1f °C",data.Temperature) }
        	{ data.GPUMemoryFree + "MB" }
        	{ string.format("%.1f %%",data.Memory) }
        	{ fsys.formatSize(data.UploadSpeed)+"/s" }
        	{ fsys.formatSize(data.DownloadSpeed)+"/s" }
        })
    },1000
)

winform.show();
win.loopMessage();


最新回复 (0)
返回