首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

為什么在多線程狀態下,反復點擊按鈕后會發生Access Violation的錯誤?该怎么处理

2012-02-19 
為什么在多線程狀態下,反復點擊按鈕后會發生Access Violation的錯誤?為什么在多線程狀態下,反復點擊按鈕后

為什么在多線程狀態下,反復點擊按鈕后會發生Access Violation的錯誤?
為什么在多線程狀態下,反復點擊按鈕后會發生Access Violation的錯誤?

在unit1窗體上畫1個Flash,和1個按鈕:

unit Unit1;

interface

uses
  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;

interface

uses
  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.


你快速點擊幾次按鈕后,就會觸發錯誤。而單線程則很穩定,為什么,該怎么解決?謝謝!

[解决办法]
不需要用线程 



[解决办法]
用线程锁控制就不会出现线程冲突了,不过要确定你是否是因为线程冲突造成的

热点排行