[急]看看我这个多线程问题 ( 积分: 0 )

  • 主题发起人 主题发起人 kofoun
  • 开始时间 开始时间
K

kofoun

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在想通过按钮,单击后,让声音发声,但发声时,窗体界面是不能动的,所以我创建了一个线程实现可以使窗体动,但有问题如下:
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,mmsystem, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
//function playsoundx:boolean;
implementation
{$R *.dfm}
function playsoundx:boolean;
var
i:integer;
begin
for i:=0 to 2do
begin
sndplaysound('c:/C:/WINDOWS/MEDIA/ding.wav',0);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Ehandle:Thandle;
begin
Ehandle:=createThread(nil,0,@playsoundx,nil,0,ThreadID);
if ThreadID=0 then
begin
playsoundx;
end;
end;
end.

底部错误如下:
[Warning] Unit1.pas(36): Return value of function 'playsoundx' might be undefined
[Error] Unit1.pas(42): Undeclared identifier: 'ThreadID'
[Warning] Unit1.pas(43): Comparing signed and unsigned types - widened both operands
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
 
我现在想通过按钮,单击后,让声音发声,但发声时,窗体界面是不能动的,所以我创建了一个线程实现可以使窗体动,但有问题如下:
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,mmsystem, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
//function playsoundx:boolean;
implementation
{$R *.dfm}
function playsoundx:boolean;
var
i:integer;
begin
for i:=0 to 2do
begin
sndplaysound('c:/C:/WINDOWS/MEDIA/ding.wav',0);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Ehandle:Thandle;
begin
Ehandle:=createThread(nil,0,@playsoundx,nil,0,ThreadID);
if ThreadID=0 then
begin
playsoundx;
end;
end;
end.

底部错误如下:
[Warning] Unit1.pas(36): Return value of function 'playsoundx' might be undefined
[Error] Unit1.pas(42): Undeclared identifier: 'ThreadID'
[Warning] Unit1.pas(43): Comparing signed and unsigned types - widened both operands
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
 
ThreadID
没有定义
 
直接用线程类就可以了
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
type
TDBThread = class(TThread)
protected
procedure playsoundx;
procedure Execute;
override;
end;

var
DBThread: TDBThread;

{$R *.DFM}

{ TDBThread }
procedure TDBThread.Execute;
var i:integer;
begin
i:=0;
while (i<2)do
begin
Synchronize(playsoundx);
Sleep(500);
end;
end;

procedure TDBThread.playsoundx;
begin
sndplaysound('c:/C:/WINDOWS/MEDIA/ding.wav',0);
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
DBThread.Terminate;
DBThread.WaitFor;
DBThread.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DBThread := TDBThread.Create(False);
end;
end.
试试这个没有测试过:).
 
后退
顶部