unit uPlaySnd;interfaceuses Classes,StdCtrls,SysUtils,Dialogs,Contnrs,MMSystem; type PFileRecord=^TFileRecord; TFileRecord=record AFilePath:String; end; type TPlayThread =Class(TThread) private APlayList:TThreadList; function GetPlayFile:string; procedure PlayWav(AFileName:string); protected procedure Execute; override; public Constructor Create(); destructor Destroy; override; procedure AddPlayFile(AFileName:string); end;implementation{ TPlayThread }constructor TPlayThread.Create(); begin inherited create(false); APlayList:=TThreadList.Create; FreeOnTerminate:=True; end;procedure TPlayThread.PlayWav(AFileName:string); begin sndPlaySound(PChar(AFileName),SND_FILENAME or SND_SYNC); end;procedure TPlayThread.Execute; var AFilename:string; begin while not self.Terminated do begin AFilename:=Self.GetPlayFile; if AFilename<>'' then begin PlayWav(AFilename); end else Sleep(10); end; end;procedure TPlayThread.AddPlayFile(AFileName: string); var AFileRecord:PFileRecord; begin with self.APlayList.LockList do begin try if Count >10 then Exit; New(AFileRecord); AFileRecord^.AFilePath:=AFileName; Add(AFileRecord); finally APlayList.UnlockList; end; end; end;function TPlayThread.GetPlayFile: string; begin Result:=''; with self.APlayList.LockList do begin try if Count =0 then Exit; Result:=PFileRecord(Items[0])^.AFilePath; PFileRecord(Items[0])^.AFilePath:=''; Dispose(PFileRecord(Items[0])); Delete(0); finally APlayList.UnlockList; end; end; end;destructor TPlayThread.Destroy; begin with self.APlayList.LockList do begin try while Count<>0 do begin PFileRecord(Items[0])^.AFilePath:=''; Dispose(PFileRecord(Items[0])); Delete(0); end; finally APlayList.UnlockList; end; end; FreeAndNil(APlayList); inherited; end;end.
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) cmdAdd: TButton; cmdRead: TButton; procedure cmdAddClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure cmdReadClick(Sender: TObject); private { Private declarations } threadList: TThreadList; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.cmdAddClick(Sender: TObject); var list:TList; str: ^string; begin list:= threadList.LockList; try new(str); str^:='hello world'; list.Add(str); finally threadList.UnlockList; end; end;procedure TForm1.FormCreate(Sender: TObject); begin threadList:= TThreadList.Create; end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); var intI: Integer; list: TList; begin list:= threadList.LockList; try for intI:= list.Count-1 downto 0 do Dispose(list.Items[intI]); finally threadList.UnlockList; end; end;procedure TForm1.cmdReadClick(Sender: TObject); var list:TList; str: ^string; intI: Integer; begin list:= threadList.LockList; try for intI:=list.Count-1 downto 0 do begin str:=Pointer(list.Items[intI]); showmessage(str^); end; finally threadList.UnlockList; end; end;end.
Classes,StdCtrls,SysUtils,Dialogs,Contnrs,MMSystem;
type
PFileRecord=^TFileRecord;
TFileRecord=record
AFilePath:String;
end;
type
TPlayThread =Class(TThread)
private
APlayList:TThreadList;
function GetPlayFile:string;
procedure PlayWav(AFileName:string);
protected
procedure Execute; override;
public
Constructor Create();
destructor Destroy; override;
procedure AddPlayFile(AFileName:string);
end;implementation{ TPlayThread }constructor TPlayThread.Create();
begin
inherited create(false);
APlayList:=TThreadList.Create;
FreeOnTerminate:=True;
end;procedure TPlayThread.PlayWav(AFileName:string);
begin
sndPlaySound(PChar(AFileName),SND_FILENAME or SND_SYNC);
end;procedure TPlayThread.Execute;
var
AFilename:string;
begin
while not self.Terminated do
begin
AFilename:=Self.GetPlayFile;
if AFilename<>'' then
begin
PlayWav(AFilename);
end else Sleep(10);
end;
end;procedure TPlayThread.AddPlayFile(AFileName: string);
var
AFileRecord:PFileRecord;
begin
with self.APlayList.LockList do
begin
try
if Count >10 then Exit;
New(AFileRecord);
AFileRecord^.AFilePath:=AFileName;
Add(AFileRecord);
finally
APlayList.UnlockList;
end;
end;
end;function TPlayThread.GetPlayFile: string;
begin
Result:='';
with self.APlayList.LockList do
begin
try
if Count =0 then Exit;
Result:=PFileRecord(Items[0])^.AFilePath;
PFileRecord(Items[0])^.AFilePath:='';
Dispose(PFileRecord(Items[0]));
Delete(0);
finally
APlayList.UnlockList;
end;
end;
end;destructor TPlayThread.Destroy;
begin
with self.APlayList.LockList do
begin
try
while Count<>0 do
begin
PFileRecord(Items[0])^.AFilePath:='';
Dispose(PFileRecord(Items[0]));
Delete(0);
end;
finally
APlayList.UnlockList;
end;
end;
FreeAndNil(APlayList);
inherited;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
cmdAdd: TButton;
cmdRead: TButton;
procedure cmdAddClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure cmdReadClick(Sender: TObject);
private
{ Private declarations }
threadList: TThreadList;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.cmdAddClick(Sender: TObject);
var
list:TList;
str: ^string;
begin
list:= threadList.LockList;
try
new(str);
str^:='hello world';
list.Add(str);
finally
threadList.UnlockList;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
threadList:= TThreadList.Create;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
intI: Integer;
list: TList;
begin
list:= threadList.LockList;
try
for intI:= list.Count-1 downto 0 do
Dispose(list.Items[intI]);
finally
threadList.UnlockList;
end;
end;procedure TForm1.cmdReadClick(Sender: TObject);
var
list:TList;
str: ^string;
intI: Integer;
begin
list:= threadList.LockList;
try
for intI:=list.Count-1 downto 0 do
begin
str:=Pointer(list.Items[intI]);
showmessage(str^);
end;
finally
threadList.UnlockList;
end;
end;end.