為什么在多線程狀態下,反復點擊按鈕后會發生Access Violation的錯誤?在unit1窗體上畫1個Flash,和1個按鈕:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, ShockwaveFlashObjects_TLB, StdCtrls,unit2;type
TForm1 = class(TForm)
Fla: TShockwaveFlash;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
public
iSwf:integer;
IsSwfBusy:boolean;
hThread:TMyThread;
end;var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if IsSwfBusy then exit;
IsSwfBusy:=true;
hThread:=TMyThread.Create(false);
hThread.FreeOnTerminate:=true;
IsSwfBusy:=true;
end;procedure TForm1.FormShow(Sender: TObject);
begin
hThread:=nil;iSwf:=0;
end;end.unit Unit2;interfaceuses
Classes,SysUtils;type
tmythread = class(TThread)
protected
procedure Execute; override;
end;implementation
uses unit1;
procedure tmythread.Execute;
begin
unit1.Form1.iSwf:=unit1.Form1.iSwf mod 2+1; //準備1.swf,2.swf兩個Flash,交替播放
unit1.Form1.Fla.Movie:='C:\'+inttostr(unit1.Form1.iSwf)+'.swf';
unit1.Form1.Fla.Play();
unit1.Form1.IsSwfBusy:=false;
end;end.
你快速點擊幾次按鈕后,就會觸發錯誤。而單線程則很穩定,為什么,該怎么解決?謝謝!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, ShockwaveFlashObjects_TLB, StdCtrls,unit2;type
TForm1 = class(TForm)
Fla: TShockwaveFlash;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
public
iSwf:integer;
IsSwfBusy:boolean;
hThread:TMyThread;
end;var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if IsSwfBusy then exit;
IsSwfBusy:=true;
hThread:=TMyThread.Create(false);
hThread.FreeOnTerminate:=true;
IsSwfBusy:=true;
end;procedure TForm1.FormShow(Sender: TObject);
begin
hThread:=nil;iSwf:=0;
end;end.unit Unit2;interfaceuses
Classes,SysUtils;type
tmythread = class(TThread)
protected
procedure Execute; override;
end;implementation
uses unit1;
procedure tmythread.Execute;
begin
unit1.Form1.iSwf:=unit1.Form1.iSwf mod 2+1; //準備1.swf,2.swf兩個Flash,交替播放
unit1.Form1.Fla.Movie:='C:\'+inttostr(unit1.Form1.iSwf)+'.swf';
unit1.Form1.Fla.Play();
unit1.Form1.IsSwfBusy:=false;
end;end.
你快速點擊幾次按鈕后,就會觸發錯誤。而單線程則很穩定,為什么,該怎么解決?謝謝!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货