- UID
- 8608
注册时间2006-2-27
阅读权限20
最后登录1970-1-1
以武会友
TA的每日心情 | 擦汗 2020-7-7 10:06 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
/////////////////////////////////////////////////////////////////////////////////
/// 这是我在一个程序中编写多线程类,主要目的就是为了能够以多线程方式顺序调用自定义函数
/// 调用方式:1. TSynThread.Create(TFunction,false,'自定义消息');
/// 2. TSynThread.Create(TObjFunction,false,'自定义消息');
/// 3. TSynThread.SaveMsg('自定义消息');
/// 请兄弟姐妹们多多指教。
///----------------------------------------------------------------------------------
/// 现在正在写一个用Excel服务器打印报表的程序,但遇到了CPU使用率很高的问题。
/// 明天把我的代码贴出来,希望对此有经验的兄弟姐妹们给予帮助。
/// 我的Email: [email protected]
/// 二○○八年六月十九日 扬州
unit uSynThread;
interface
uses Classes,forms,SysUtils,inifiles,SyncObjs,uGVar;
type
TFunction = function:boolean;
TObjFunction = function:boolean of object;
TSynThread=Class(TThread)
private
procedure CreateSelf(CreateSuspended: Boolean);
procedure DoCallFun;
protected
FSynNo:integer;
FMsg:string;
FCallFun:TFunction;
FCallObjFun:TObjFunction;
procedure Execute;override;
public
constructor Create(CreateSuspended: Boolean);overload;
constructor Create(Fun:TFunction;CreateSuspended:boolean;msg:String);overload;
constructor Create(ObjFun:TObjFunction;CreateSuspended:boolean;msg:String);overload;
Constructor SaveMsg(msg:string);
destructor Destroy;override;
end;
var
SynThread:TSynThread;
procedure SaveErrMsg(ErrMsg:string);
implementation
var
CSynMsg:string='';
gSynNo:integer=0;
gSynCount:integer=0;
procedure SaveErrMsg(ErrMsg:string);
begin
with TIniFile.Create( ChangeFileExt( Application.ExeName, '.Err' )) do begin
try
WriteString('SYSTEM ERROR',DateTimetostr(now),ErrMsg);
finally
free;
end;
end;
end;
{ TInitThread }
constructor TSynThread.Create(CreateSuspended: Boolean);
begin
CreateSelf(CreateSuspended);
end;
constructor TSynThread.Create(Fun: TFunction; CreateSuspended: boolean;msg:String);
begin
CreateSelf(CreateSuspended);
FMsg:=msg;
FCallFun:=Fun;
FCallObjFun:=nil;
end;
constructor TSynThread.Create(ObjFun: TObjFunction;
CreateSuspended: boolean; msg: String);
begin
CreateSelf(CreateSuspended);
FMsg:=msg;
FCallFun:=nil;
FCallObjFun:=ObjFun;
end;
procedure TSynThread.CreateSelf(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
Priority := tpIdle;
FreeOnTerminate:=True;
FSynNo:=gSynCount;
gSynCount:=gSynCount+1;
end;
destructor TSynThread.Destroy;
begin
inherited;
end;
procedure TSynThread.DoCallFun;
begin
Try
CSynMsg:=FMsg;
if Assigned(FCallFun) then begin
FCallFun();
end;
if Assigned(FCallObjFun) then begin
FCallObjFun();
end;
if (not Assigned(FCallObjFun)) and (not Assigned(FCallFun)) then begin
SaveErrMSG(FMsg);
end;
except
on E: Exception do begin
SaveErrMSG(FMsg+' ErrCode='+inttostr(E.HelpContext)+':'+e.Message);
end;
end;
end;
procedure TSynThread.Execute;
begin
while not terminated do begin
if FSynNo<=gSynNo then break;
Application.ProcessMessages;
sleep(10);
end;
/// 以上循环原本使用的是TEvent的,但我在使用时发生了一些意想不到的问题
/// 因此采用了这个结构来顺序调用程序。
Application.ProcessMessages;
Synchronize(DoCallFun);
SynThread:=nil;
gSynNo:=gSynNo+1;
end;
constructor TSynThread.SaveMsg(msg: string);
begin
CreateSelf(false);
FMsg:=msg;
FCallFun:=nil;
FCallObjFun:=nil;
end;
end.
|
|