以下是包含这个临界区的类代码,
MsgNotify 对象是全局对象,只有一个实例,
其他线程使用它的 DoMessage 方法来更新界面,
所以使用了临界区对象
TMsgNotify = class(TObject)
private
FCS: TRTLCriticalSection;
FMsgStr: string;
FMsgType: TMsgType;
FOnMessage: TDeviceMsgNoti#yEvent;
public
constructor Create;
destructor Destroy; override;
procedure DoMessage(Sender: TObject;const IDevice:IMsgNotityDevice;
AMsgType:TMsgType;AMsgStr:string);
property MsgStr: string read FMsgStr;
property MsgType: TMsgType read FMsgType;
property OnMessage: TDeviceMsgNoti#yEvent read FOnMessage write FOnMessage;
end;
{
********************************** TMsgNotify **********************************
}
constructor TMsgNotify.Create;
begin
inherited;
//初始化阻塞对象
InitializeCriticalSection(FCS);
end;
destructor TMsgNotify.Destroy;
begin
//删除阻塞对象
DeleteCriticalSection(FCS);
inherited;
end;
procedure TMsgNotify.DoMessage(Sender: TObject;const IDevice:IMsgNotityDevice;
AMsgType:TMsgType;AMsgStr:string);
begin
//开始阻塞