如果不能正常运行,请下载最新版本 vlistEx 和 http 库。 下载地址:光庆·程序·在线 |
import win.ui;
import godking.vlistEx;
import thread.works
/*DSG{{*/
var mainForm = win.form(text="aardio form";right=1031;bottom=551)
mainForm.add(
button={cls="button";text="停止下载";left=296;top=480;right=448;bottom=528;z=2};
button2={cls="button";text="开始下载";left=520;top=480;right=672;bottom=528;z=3};
vlist={cls="vlistEx";left=10;top=10;right=1024;bottom=464;db=1;dl=1;dr=1;dt=1;edge=1;transparent=1;z=1}
)
/*}}*/
var t = { fields={"序号","url","保存文件","下载进度"} };
for(i=1;50;1){
..table.push(t,{"序号"="[@rowindex]"});
}
mainForm.vlist.setTable(t,/*列标题*/,{scale=true,0.5,5,3,2}/*列宽*/,1/*对齐方式*/,/*字典转为数组*/,/*字符串转为Utf8*/,true/*是否克隆新表*/);
mainForm.vlist.fillParent();
..io.createDir("\down");
import thread.command
thread.command.instance().setinfo = function(row,v,url,file){
if #url mainForm.vlist.setCellText(row,2,url);
if #file mainForm.vlist.setCellText(row,3,file);
mainForm.vlist.setCellText(row,4,v:0);
}
var work = thread.works(5,
function(param) {
if thread.var("stop").get() return ;
import godking.http;
import thread.command;
thread.command.setinfo(param.index,"开始 "++param.index,param.url);
var t,n={info = "超时或终止"},0;
var callback = function(id,v,all,url,file){
thread.command.setinfo(id,..math.round((v:0)*100/all,2)++"%",url,file);
return thread.var("stop").get();
};
do{
n++;
if n>3 or thread.var("stop").get() break; // 下载失败重试,共3次后结束。
_,_,t = godking.http({
url = param.url;
callback = callback;
callbackID = param.index;
file = "\down\QQ_"+param.index+".exe";
fileResume = true;
})
} while ( !t.success and t.info!=="下载被终止" )
if t.success===true {
thread.command.setinfo(param.index,"下载完毕",param.url,t.file);
} elseif t.success===false {
thread.command.setinfo(param.index,"下载失败:"++(t.info:"")++..math.round(t.downsize*100/t.allsize,2)++"%",param.url,t.file);
} else {
thread.command.setinfo(param.index,"下载失败:"++(t.info:""));
}
return ;
}
);
mainForm.show();
mainForm.button.oncommand = function(id,event){
thread.var("stop").set(true);
}
mainForm.button2.oncommand = function(id,event){
thread.var("stop").set(false);
mainForm.vlist.setTable(t,/*列标题*/,{scale=true,0.5,5,3,2}/*列宽*/,1/*对齐方式*/,/*字典转为数组*/,/*字符串转为Utf8*/,true/*是否克隆新表*/);
for(i=1;50;1){
// ★★★★★ 需要特别注意的知识点:
// 已添加的工作未执行完毕前,本循环会被打断,处于等待状态,直到works需要获取新工作时,再继续执行,给works添加新工作。
// 所以,如果需要停止全部工作时,则不要再继续添加。
if thread.var("stop").get() return ;
work.push({index=i,url="https://dldir1.qq.com/qqfile/qq/QQNT/Windows/QQ_9.9.15_240902_x86_01.exe"});
}
}
mainForm.onClose = function(hwnd,message,wParam,lParam){
import process
process().terminate();
}
// 定时刷新虚表
mainForm.vlist.autoRedraw = false;
mainForm.setInterval(
function(){
mainForm.vlist.redraw(true)
},200
)
// 添加工作任务
mainForm.button2.oncommand()
win.loopMessage();
注意:
有些系统可能默认最大连接数为2,也就是最多同时下载两个。可以用下面的函数修改后在下载:
import godking.http;
godking.http.setMaxConnections(10)