Changeset 245


Ignore:
Timestamp:
May 23, 2011, 10:59:04 AM (13 years ago)
Author:
george
Message:
  • Added: Thread safe global thread list management.
  • Added: DeleteFiles routine.
  • Modified: GetUserNameEx use dynamic loaded library.
Location:
Common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Common/UCommon.pas

    r240 r245  
    11unit UCommon;
    22
     3{$mode delphi}
     4
    35interface
    46
    57uses
    6   Windows, Classes, SysUtils, SpecializedList, StrUtils, Dialogs; //, ShFolder, ShellAPI;
     8  Windows, Classes, SysUtils, SpecializedList, StrUtils, Dialogs,
     9  FileUtil; //, ShFolder, ShellAPI;
    710
    811type
     
    2528var
    2629  ExceptionHandler: TExceptionEvent;
     30  DLLHandle1: HModule;
     31  GetUserNameEx: procedure (NameFormat: DWORD;
     32    lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
     33
    2734
    2835function IntToBin(Data: Cardinal; Count: Byte): string;
     
    4552function GetFileFilterItemExt(Filter: string; Index: Integer): string;
    4653procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
     54procedure DeleteFiles(APath, AFileSpec: string);
    4755
    4856
    4957implementation
     58
     59procedure DeleteFiles(APath, AFileSpec: string);
     60var
     61  SearchRec: TSearchRec;
     62  Find: Integer;
     63  Path: string;
     64begin
     65  Path := IncludeTrailingPathDelimiter(APath);
     66
     67  Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
     68  while Find = 0 do begin
     69    DeleteFile(Path + SearchRec.Name);
     70
     71    Find := SysUtils.FindNext(SearchRec);
     72  end;
     73  FindClose(SearchRec);
     74end;
     75
    5076
    5177function GetFileFilterItemExt(Filter: string; Index: Integer): string;
     
    226252end;
    227253
    228 procedure GetUserNameEx(NameFormat: DWORD;
    229   lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
    230   external 'secur32.dll' Name 'GetUserNameExA';
    231 
     254function GetVersionInfo: TOSVersionInfo;
     255begin
     256  Result.dwOSVersionInfoSize := SizeOf(Result);
     257  if GetVersionEx(Result) then begin
     258  end;
     259end;
    232260
    233261function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    234262var
    235263  UserName: array[0..250] of Char;
     264  VersionInfo: TOSVersionInfo;
    236265  Size: DWORD;
    237266begin
    238   Size := 250;
    239   GetUserNameEx(Integer(Format), @UserName, @Size);
    240   Result := UTF8Encode(UserName);
     267  VersionInfo := GetVersionInfo;
     268  if VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
     269    Size := 250;
     270    GetUserNameEx(Integer(Format), @UserName, @Size);
     271    Result := UTF8Encode(UserName);
     272  end else Result := GetUserName;
    241273end;
    242274
     
    272304end;
    273305
     306procedure LoadLibraries;
     307begin
     308  DLLHandle1 := LoadLibrary('secur32.dll');
     309  if DLLHandle1 <> 0 then
     310  begin
     311    @GetUserNameEx := GetProcAddress(DLLHandle1, 'GetUserNameExA');
     312  end;
     313end;
     314
     315procedure FreeLibraries;
     316begin
     317  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
     318end;
     319
     320
     321initialization
     322
     323LoadLibraries;
     324
     325
     326finalization
     327
     328FreeLibraries;
     329
    274330end.
  • Common/UThreading.pas

    r230 r245  
    66
    77uses
    8   Classes, SysUtils, Forms, Contnrs;
     8  Classes, SysUtils, Forms, Contnrs, SyncObjs;
    99
    1010type
    11 
     11  TExceptionEvent = procedure (Sender: TObject; E: Exception) of object;
    1212  TMethodCall = procedure of object;
    13 
    14   { TTermThread }
    15 
    16   TTermThread = class(TThread)
    17     Finished: Boolean;
    18     Method: TMethodCall;
    19     procedure Execute; override;
    20   end;
    2113
    2214  { TListedThread }
     
    2820    destructor Destroy; override;
    2921    procedure Sleep(Delay: Integer);
     22    property Terminated;
     23  end;
     24
     25  { TTermThread }
     26
     27  TTermThread = class(TListedThread)
     28  private
     29  public
     30    Finished: Boolean;
     31    Method: TMethodCall;
     32    procedure Execute; override;
    3033  end;
    3134
    3235var
    33   ThreadList: TObjectList; // TListedThread
     36  ThreadList: TObjectList; // TList<TListedThread>
     37  ThreadListLock: TCriticalSection;
     38  OnException: TExceptionEvent;
    3439
    3540procedure RunInThread(Method: TMethodCall);
    3641procedure Synchronize(Method: TMethodCall);
     42
     43resourcestring
     44  SCurrentThreadNotFound = 'Current thread ID %d not found in list.';
    3745
    3846
     
    4957    Thread.Method := Method;
    5058    while not Thread.Finished do begin
    51       Application.ProcessMessages;
     59      if MainThreadID = ThreadID then Application.ProcessMessages;
    5260      Sleep(1);
    5361    end;
     
    7078      Thread := TListedThread(ThreadList[I]);
    7179      TThread.Synchronize(Thread, Method);
    72     end else raise Exception.Create(Format('Current thread ID %d not found in list.', [ThreadID]));
     80    end else raise Exception.Create(Format(SCurrentThreadNotFound, [ThreadID]));
    7381  end;
    7482end;
     
    8088begin
    8189  inherited;
    82   ThreadList.Add(Self);
     90  try
     91    ThreadListLock.Acquire;
     92    ThreadList.Add(Self);
     93  finally
     94    ThreadListLock.Release;
     95  end;
    8396end;
    8497
    8598destructor TListedThread.Destroy;
    8699begin
    87   ThreadList.Delete(ThreadList.IndexOf(Self));
     100  if not Suspended then
     101  begin
     102    Terminate;
     103    WaitFor;
     104  end;
     105  try
     106    ThreadListLock.Acquire;
     107    ThreadList.Delete(ThreadList.IndexOf(Self));
     108  finally
     109    ThreadListLock.Release;
     110  end;
    88111  inherited Destroy;
    89112end;
     
    106129procedure TTermThread.Execute;
    107130begin
    108   Method;
    109   Finished := True;
     131  try
     132    Method;
     133    Finished := True;
     134  except
     135    on E: Exception do
     136      if Assigned(OnException) then
     137        OnException(Self, E);
     138  end;
    110139end;
    111140
    112141initialization
    113142
     143ThreadListLock := TCriticalSection.Create;
    114144ThreadList := TObjectList.Create;
    115145ThreadList.OwnsObjects := False;
     
    118148
    119149ThreadList.Free;
     150ThreadListLock.Free;
    120151
    121152end.
Note: See TracChangeset for help on using the changeset viewer.