事件为参数线程怎么写
现在有很多FORM上的查询事件 button1click,button1click,button1click,查询时间比较长经常会出现白屏的情况.
想写个通用线程,参数为click事件,只要传参数 button1.click,就可以执行查询动作,大家有什么好的办法.
[解决办法]
写个简单的例子吧, 只需要把需要执行的过程(不能带参数)传给线程的ExecuteMethod方法就行。
Circled = True 表示循环执行, False 表示只运行一次。
但是还是会出现界面卡的情况,因为要同步VCL,所以如果涉及到界面的一些操作,用线程意义不大。
- Delphi(Pascal) code
type TWorkThread = class(TThread) private FExecuteMethod: TThreadMethod; FCircled: Boolean; protected procedure Execute; override; public constructor Create; property ExecuteMethod: TThreadMethod read FExecuteMethod write FExecuteMethod; property Circled: Boolean read FCircled write FCircled default False; end;{ TWorkThread }constructor TWorkThread.Create;begin FCircled := False; FExecuteMethod := nil; inherited Create(False);end;procedure TWorkThread.Execute;begin FreeOnTerminate := True; while not Terminated do begin if Assigned(FExecuteMethod) then begin Synchronize(FExecuteMethod); if not FCircled then Break; end; Sleep(10); end;end;
[解决办法]
把对象传递过去不就可以了么