unit MCAD_Event; interface uses Windows, Classes, SyncObjs, xSyncObjs; const cMainThreadName='MI1201_Thread'; type tFlag=(fWaitForever, fCloseHandle, fAlreadyExist); tFlags=set of tFlag; (* tMCADThreadEventX=class(TEvent) private LocCounter:cardinal; Tread:dword; prFlags:tFlags; protected procedure WaitForeverSet(state:boolean); function WaitForeverGet:boolean; public constructor Create; destructor Destroy; override; function WaitFor(Timeout: DWORD): TWaitResult; function WaitForLimitedTime(Timeout: DWORD): TWaitResult; function ResetEvent:boolean; function SetEvent:boolean; function SetEventAnyway:boolean; function Owner:boolean; function NotLocked:boolean; procedure MakeOwner; property WaitForever:boolean read WaitForeverGet write WaitForeverSet default false; end; *) tMCADThreadEvent=class(TMutex) constructor Create; end; implementation USES SysUtils; constructor tMCADThreadEvent.Create; begin Inherited Create(NIL, FALSE, cMainThreadName); end; //----------------------------------------------------------------------- (*constructor tMCADThreadEventX.Create; begin Inherited Create(NIL, FALSE, FALSE, cMainThreadName); Tread:=0; LocCounter:=0; prFlags:=[]; end; destructor tMCADThreadEventX.Destroy; begin Inherited Destroy; end; function tMCADThreadEventX.WaitFor; begin if Owner then begin Result:=wrSignaled; Inc(LocCounter); end else begin Result:= inherited WaitFor(TimeOut); while (Result=wrTimeOut) and (fWaitForever in prFlags) do begin Result:=inherited WaitFor(TimeOut); end; if Result=wrSignaled then begin MakeOwner; Exclude(prFlags,fWaitForever); Inc(LocCounter); end; end; end; function tMCADThreadEventX.WaitForLimitedTime; begin if Owner then begin Result:=wrSignaled; Inc(LocCounter); end else begin Result:= inherited WaitFor(TimeOut); if Result=wrSignaled then begin MakeOwner; Exclude(prFlags,fWaitForever); Inc(LocCounter); end; end; end; function tMCADThreadEventX.ResetEvent; begin if Owner or NotLocked then begin MakeOwner; Result:=TRUE; Inc(LocCounter); WaitForever:=FALSE; Inherited ResetEvent; end else begin Result:=FALSE; end; end; function tMCADThreadEventX.SetEvent; begin Result:=FALSE; if Owner or NotLocked then begin if LocCounter>0 then Dec(LocCounter); if LocCounter=0 then begin WaitForever:=False; Tread:=0; Result:=TRUE; Inherited SetEvent; end; end; end; function tMCADThreadEventX.SetEventAnyway; begin Result:=FALSE; if Owner or NotLocked then begin WaitForever:=False; Inherited SetEvent; Tread:=0; Inherited SetEvent; Result:=TRUE; end; end; procedure tMCADThreadEventX.WaitForeverSet; begin if Owner then begin if state then Include(prFlags,fWaitForever) else Exclude(prFlags,fWaitForever) end; end; function tMCADThreadEventX.WaitForeverGet; begin Result:=fWaitForever in prFlags; end; function tMCADThreadEventX.Owner; begin Result:=(Tread=GetCurrentThreadId()); end; function tMCADThreadEventX.NotLocked; begin Result:=(Tread=0); end; procedure tMCADThreadEventX.MakeOwner; begin Tread:=GetCurrentThreadId(); end; *) end.