//command 线程命令
import win;
import thread.table;
namespace thread;

class command{
	ctor( winform ){ 
		this._form = winform;
		this._commands = {};
		
		winform.wndproc = {
			[ 0x4CC/*_WM_THREAD_COMMAND*/ ]  = function(hwnd, message, wParam, lParam){ 
        		receive( this._commands,wParam) 
			};  
		}
		..table.add_gc(this,function(){
			for(name in this._commands ){
				 this[name] = null;
			} 
		});
	};
	@_meta;
}
 
namespace command {  
	var commandTable = ..thread.table("{3EA9E65D-B101-42AF-93D7-08522F8841CD}")
 	
 	_meta = {
		_set = function(k,v){
			commandTable.setByNamespace("subscribe['"+k+"'][" + owner._form.hwnd + "]",v?true:null)
			owner._commands[k] = v;
		}
	};
 	var sendCmd = function( sendApi,hwnd,method,... ){  
 		var tHwnd = type(hwnd); 
		if( type(hwnd) == type.string ){
			realName = hwnd;
			var subscribe = commandTable.getByNamespace("subscribe['"+realName+"']")
			for(hwnd in subscribe){
				var id = commandTable.push({realName;method;...}); 
				sendApi(hwnd,0x4CC/*_WM_THREAD_COMMAND*/,id,0)
			} 
		}
		elseif(tHwnd == type.number){
			var id = commandTable.push({method;...}); 
			return sendApi( hwnd,0x4CC/*_WM_THREAD_COMMAND*/,id,0);
		};
	}
 	
	send = function( ... ){
		sendCmd(::SendMessageInt,... );
	}
	
	post = function( ... ){
		sendCmd(::PostMessage,... );
	} 
	
	receive = function( methodTable,wParam ){ 
		var args = commandTable.get(wParam) 
		commandTable.set(wParam,null); 
		var action = ..table.remove(args); 
		var method = methodTable[action]
		if(type(method)!="function")
			error("未找到指定方法");
		invoke(method,commandTable, ..table.unpack( args) )	
	} 
}

/**intellisense()
thread.command.post(.(窗口句柄,"命令函数名",其他参数) = 不阻塞调用跨线程命令 
thread.command.post(.("命令函数名",其他参数) = 不阻塞调用跨线程命令,\n不指定窗口句柄,所有同名的线程命令函数都会被调用
thread.command.send(.(窗口句柄,"命令函数名",其他参数) = 阻塞调用跨线程命令
thread.command.send(.("命令函数名",其他参数) = 阻塞调用跨线程命令\n不指定窗口句柄,所有同名的线程命令函数都会被调用
thread.command(winform__) = 创建线程命令对象\n该对象定义的成员函数,都可在其他线程中用 thread.command.post()调用
thread.command() = !thread_command.
!thread_command.自定义函数名 = @.命令函数 = function(__/*支持不定个数参数*/){
	
}
_WM_THREAD_COMMAND=@0x4CC/*_WM_THREAD_COMMAND*/
end intellisense**/