
使用thread.manage来管理多线程,使用plus来绘制下载的可视化界面,用线程表在界面与线程池之间传递数据。
Code AardioLine:102复制
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.import
win
.ui;var
winform = win
.form(text="多线程下载的图形化界面"
;right=759
;bottom=469
)- winform.add(
- btnPause={cls=
"button"
;text="暂停所有线程"
;left=16
;top=88
;right=160
;bottom=133
;dl=1
;dt=1
;z=2
}; - btnStart={cls=
"button"
;text="启动多线程下载"
;left=16
;top=24
;right=160
;bottom=69
;dl=1
;dt=1
;z=1
}; - btnStop={cls=
"button"
;text="停止所有线程"
;left=16
;top=152
;right=160
;bottom=197
;dl=1
;dt=1
;z=3
}; - plus={cls=
"plus"
;left=176
;top=24
;right=736
;bottom=448
;clipBk=1
;db=1
;dl=1
;dr=1
;dt=1
;edge=1
;notify=1
;repeat="center"
;z=4
} - )
import
thread
.manage; - manage =
thread
.manage(5
) var
totalSize = 1000
;var
rowNum = 40
; var
colNum = totalSize / rowNum; import
thread
.table
; var
tt_DoneColor = thread
.table
(); var
ini_threadtables = function
(){-
for
(i=1
; totalSize; 1
){ - tt_DoneColor[i] =
null
; - }
- }
var
downloading = function
(arr1,color,startNum,endNum,pieceSize){-
if
(!endNum) endNum = startNum + pieceSize -1
; -
for
(i=startNum; endNum; 1
){ -
sleep
(math
.random(30
,300
)) - arr1[i]= color;
- }
- }
- winform.btnStart.oncommand =
function
(id,event){ -
if
(manage.busy()==true
) return
; - ini_threadtables(); iscancle =
false
; -
-
-
var
pieceSize = totalSize / 5
- manage.create(downloading, tt_DoneColor, 0xEEE080E0,
1
,, pieceSize) - manage.create(downloading, tt_DoneColor, 0xEE65C200, pieceSize+
1
,, pieceSize) - manage.create(downloading, tt_DoneColor, 0xEEFFC212, pieceSize*
2
+1
,, pieceSize) - manage.create(downloading, tt_DoneColor, 0xEEFF4100, pieceSize*
3
+1
,, pieceSize) - manage.create(downloading, tt_DoneColor, 0xEEBDB76B, pieceSize*
4
+1
,, pieceSize) -
- manage.waitClose()
-
if
(not
iscancle) win
.msgbox("下载结束"
,"提示"
) - }
- winform.btnPause.oncommand =
function
(id,event){ -
if
(manage.busy()==false
) return
; -
if
(owner
.text =="暂停所有线程"
){ - manage.suspend();
-
owner
.text = "恢复所有线程"
- }
else
{ - manage.resume();
-
owner
.text = "暂停所有线程"
- }
- }
- winform.btnStop.oncommand =
function
(id,event){ -
if
(manage.busy()==false
) return
; -
var
handles = manage.getHandles(); -
for
(i=1
; -
thread
.terminate(handles[i],0
) - }
- iscancle =
true
; -
win
.msgbox("下载已取消"
,"提示"
) - winform.btnPause.text =
"暂停所有线程"
- }
- winform.plus.onDrawContent =
function
(graphics,rc,txtColor,rcContent,foreColor){ - graphics.clear(0xFFFFFFFF)
-
-
var
w, h = rcContent.width(), rcContent.height(); -
var
cell_w , cell_h = w / rowNum, h / colNum; -
var
brush = gdip.solidBrush() -
for
(i=1
; totalSize; 1
){ -
var
x =(i-1
) % rowNum; -
var
y = math
.floor((i-1
)/rowNum); - brush.color = tt_DoneColor[i] : 0x55CCCC00;
- graphics.fillRectangle(brush, x*cell_w+
1
, y*cell_h+1
, cell_w-1
, cell_h-1
); - }
-
return
true
; - }
- winform.plus.setInterval(
-
function
(){ - winform.plus.redraw();
- },
100
- )
- winform.show();
win
.loopMessage();