//定时执行线程任务

import win.ui;
/*DSG{{*/
var winform = win.form(parent=...; text="使用thread.event创建定时执行任务的线程";right=349;bottom=211 )
winform.add( 
trackbar={ bottom=82;max=1000;text="trackbar";left=15;top=52;z=2;right=319;min=500;cls="trackbar" };
lbTip={ bottom=103;right=100;left=26;text="";top=85;transparent=1;z=3;cls="static" };
static={ bottom=44;align="center";text="";left=138;top=13;center=1;z=5;right=309;edge=1;cls="static" };
btnStart={ bottom=155;right=181;left=61;top=113;z=1;text="启动定时线程";cls="button" };
btnStop={ disabled=1;bottom=155;right=312;left=192;top=113;z=4;text="结束定时线程";cls="button" }
)
/*}}*/

task_t = function(hwnd,ms){
	import win;
	import thread.event;
	
	var evt = thread.event("定时事件 GUID:32DA0FC7-A96D-4850-9A28-DA1DD4464B44")
	while( ! evt.wait(ms) ){ 
		win.setText(hwnd,tostring( time() ) )
	}
	io.print("任务已完成")
}

import thread.event;
var evtTask = thread.event("定时事件 GUID:32DA0FC7-A96D-4850-9A28-DA1DD4464B44",false)

winform.btnStart.oncommand = function(id,event){

	winform.btnStart.disabled = true;
	winform.btnStop.disabled = false;
	winform.trackbar.disabled = true;
	
	hThread = thread.create(task_t,winform.static.hwnd,winform.trackbar.pos);
	thread.waitOne(hThread)
	
	winform.btnStart.disabled = false;
	winform.btnStop.disabled = true;
	winform.trackbar.disabled = false;
}

winform.btnStop.oncommand = function(id,event){
	evtTask.set();//使事件对象切换为有信号状态,使wait函数退出
}

winform.trackbar.oncommand = function(id,event,pos){
	winform.lbTip.text = owner.pos + "毫秒"
}
 
winform.show() 
win.loopMessage();
