//event 事件同步对象
namespace thread;

class event {
	ctor( eventName,manualReset = false,initialState=false ){ 
	
		if(type(eventName)!= type.string)
			error("参数一必须是字符串"); 
		  
		this.handle  =  OpenEvent( 0x1F0003/*_EVENT_ALL_ACCESS*/,false ,eventName);  
		if(! this.handle ) {  
			this.handle  =  CreateEvent(null,manualReset,initialState,eventName); 
		} 
		else {
			this.conflict = "信号量已存在" 
		} 
		
		if(!this.handle)
			return null,..lasterr()
		
		..table.add_gc( this,"close" );
	};
	close = function(){ 
		//The system closes the handle automatically when the process terminates
		//The event object is destroyed when its last handle has been closed
		..raw.closehandle(this.handle)
	};
	@_metaProperty; 
}
 



namespace event{
	import util.metaProperty;
	_metaProperty = util.metaProperty(
 
		_topointer = function(){
			return owner.handle;
		}; 
		set = function(){
			SetEvent( owner.handle )
		};
		
		reset = function(){
			ResetEvent( owner.handle )
		};
		
		pulse = function(){
			PulseEvent( owner.handle )
		};
		
		wait = function(timeout){
			return ..thread.wait(owner.handle,timeout)
		};  
		
		waitOne = function(timeout){
			return ..thread.waitOne(owner.handle,timeout)
		};  
	)
	
	//声明API
	CreateEvent= ::Kernel32.api("CreateEvent","pointer(pointer lpEventAttributes,bool manualReset,bool initialState,string lpName)");
	OpenEvent= Kernel32.api("OpenEvent","pointer(INT desiredAccess,bool bInheritHandle,string lpName)");
	SetEvent= Kernel32.api("SetEvent","bool(pointer hEvent)");
	PulseEvent= Kernel32.api("PulseEvent","bool(pointer hEvent)");
	ResetEvent= Kernel32.api("ResetEvent","bool(pointer hEvent)");  
}
 
/**intellisense() 
thread.event("事件对象名称__") = 创建或打开已存在的命名事件对象\n参数( 名称,是否手动复原信号状态,初始状态0\n参数2,参数3是可选参数,仅在创建新事件对象是起作用.
!thread_event.close() = 关闭事件对象\n该函数并不关闭信号量内核对象\n当所有引用内核对象的对象关闭,内核对象自动释放\n如果没有手工调用此函数,则线程结束时自动调用.
!thread_event.conflict = 如果事件对象已存在,此属性为真值\n否则为空值
!thread_event.set() = 设置事件的状态为有信号状态，\n退出所有等待该事件对象信号的函数
!thread_event.reset() = 事件对象设置为无信号状态\n如果创建事件对象时并未设定手动复位，此函数不需要手动调用.
!thread_event.pulse() = 设置事件对象为有信号状态,并释放某些等待线程\n然后设置该事件无效（无信号状态）
!thread_event.wait() = 等待事件到有信号状态,\n可选增加一个参数指定超时,以毫秒为单位\n在UI线程中应使用非阻塞的waitOne()函数替代
!thread_event.waitOne() = 等待事件到有信号状态,\n可选增加一个参数指定超时,以毫秒为单位
?thread.event = !thread_event.
thread.event = 事件同步对象支持库\n可跨进程、跨线程使用.
end intellisense**/
