国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

支持多線程的日志類,可以在線程中使用,用來與主窗口通訊

2019-11-18 18:03:37
字體:
來源:轉載
供稿:網友
 

在一些涉及到多線程的程序設計中,線程常常有一些消息要發送到用戶界面進行顯示。這方面處理的方法很多,有通過消息傳遞、全局變量、管道等。這里給出了一種通過消息傳遞和全局變量相結合的處理的方法。并且把代碼封裝到一個TLog類中,使用方便。在這里和大家分享。

//TLog類說明部分,事實上該類是一個“日志隊列”
const
  MAX_LOG_LEN=1024;
 
type
  TLog=class
  PRivate
    FLock:TCriticalSection;//互斥類,用于線程互斥訪問
    //定義日志項循環隊列--由于隊列的特性,最多能保存MAX_LOG_LEN-1條日志
    FLines:array [0..MAX_LOG_LEN-1] of string;
    FHead:integer;
    FTail:integer;
    //環隊列定義結束
    FMsgHandle:THandle;//消息接收窗口句柄
    FMsgId:integer;//消息ID
    FMsgParam:integer;//消息參數,可以用來區分不同的日志對象
    function GetIsEmpty: boolean;
    function GetCount: integer;
  public
    constructor Create(const MsgHandle:THandle;const MsgId,MsgParam:integer);
    destructor Destroy;override;
    procedure Add(const line:string);
    procedure Gets(lines:TStrings);
    procedure Clear;
    property IsEmpty:boolean read GetIsEmpty;
    property Count:integer read GetCount;
  end;


//TLog類實現部分

{ TLog }

constructor TLog.Create(const MsgHandle: THandle; const MsgId,
  MsgParam: integer);
begin
  FHead:=0;
  FTail:=0;
  FMsgHandle:=MsgHandle;
  FMsgId:=MsgId;
  FMsgParam:=MsgParam;
  FLock:=TCriticalSection.Create;
end;

destructor TLog.Destroy;
begin
  FLock.Free;
  inherited;
end;

procedure TLog.Add(const line: string);
begin
  FLock.Enter;
  try
    FLines[FTail]:=Format('[%s] %s',[FormatDateTime('hh:nn:ss',Now),line]);
    FTail:=(FTail+1) mod MAX_LOG_LEN;
    if FHead=FTail then FHead:=(FHead+1) mod MAX_LOG_LEN;//如果隊列滿,則沖掉頭部數據
  finally
    FLock.Leave;
  end;
  PostMessage(FMsgHandle,FMsgId,FMsgParam,0);//注意此處未使用SendMessage
end;

procedure TLog.Gets(lines: TStrings);
begin
  FLock.Enter;
  try
    while FTail<>FHead do
    begin
      lines.Add(FLines[FHead]);
      FHead:=(FHead+1) mod MAX_LOG_LEN;
    end;
  finally
    FLock.Leave;
  end; 
end;

procedure TLog.Clear;
begin
  FLock.Enter;
  try
    FHead:=0;
    FTail:=0;
  finally
    FLock.Leave;
  end;
end;

function TLog.GetCount: integer;
begin
  FLock.Enter;
  try
    if FTail>FHead then
      result:=FTail-FHead
    else
      result:=FTail+MAX_LOG_LEN-FHead;
  finally
    FLock.Leave;
  end;
end;

function TLog.GetIsEmpty: boolean;
begin
  FLock.Enter;
  try
    result:=FHead=FTail;//頭尾重合時為空
  finally
    FLock.Leave;
  end;
end; 

原代碼:LogUtils.pas

TLog的使用示例

1.消息定義和處理

在主窗口定義消息,如下
const
  CM_MYLOG=WM_USER+101; 

type
  TForm1 = class(TForm)
    ....
    procedure DoMyLog(var msg:TMessage);message CM_MYLOG;//消息相應
    ....
  end;

消息處理代碼
procedure TForm1.DoMyLog(var msg: TMessage);
var
  lines:TStrings;
begin
  if(msg.WParam=0) then//注意,這里的0與TLog建立時傳入的MsgParam對應
  begin
    if not FLog.IsEmpty then
    begin
      lines:=TStringList.Create;
      try
        FLog.Gets(lines);
        Memo1.Lines.AddStrings(lines);//假設輸出到Memo1中
      finally
        lines.Free;
      end;
    end;
  end;
end;

2.TLog對象定義,初始化/釋放

在主窗口或全局中定義變量
FLog:TLog;

一般在窗口建立時生成對象
procedure TForm1.FormCreate(Sender: TObject);
begin
  ...
  FLog:=TLog.Create(Handle,CM_MYLOG,0);//注意,這里的MsgParam=0
  ...
end;

在窗口銷毀時釋放對象
procedure TForm1.FormDestroy(Sender: TObject);
begin
  ...
  FLog.Free;//注意釋放前請確認線程中已經不再使用(或者線程已經終止)
  ...
end;

3.在線程中使用TLog

  ...
  FLog.Add('具體的日志');//FLog可以在線程創建時,賦值給該線程的一個成員變量


上一篇:點擊DBGrid的Title對查詢結果排序

下一篇:軟件啟動畫面中啟動狀態的顯示

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 天津市| 天柱县| 广南县| 四平市| 石泉县| 卫辉市| 杨浦区| 克什克腾旗| 商城县| 延津县| 望谟县| 郎溪县| 巴南区| 措美县| 云林县| 铁力市| 大宁县| 伽师县| 新龙县| 吴江市| 田阳县| SHOW| 南木林县| 闽侯县| 自贡市| 石林| 安新县| 乌海市| 西青区| 昔阳县| 怀柔区| 喜德县| 常德市| 泰和县| 舟曲县| 资源县| 绥阳县| 通海县| 东方市| 尤溪县| 仙居县|