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

为什么简单的多线程会抛出EAccessViolation异常呢

2012-09-13 
为什么简单的多线程会抛出EAccessViolation错误呢?就一个很简单的多线程例子调用类:unit Unit1interfaceu

为什么简单的多线程会抛出EAccessViolation错误呢?
就一个很简单的多线程例子

调用类:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,uMyThread;

type
  TForm1 = class(TForm)
  Button1: TButton;
  procedure Button1Click(Sender: TObject);
  private
  { Private declarations }
  public
  myThread:MyThread;
  end;

var
  Form1: TForm1;



implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
myThread:=MyThread.Create(true);
myThread.Resume;
end;

end.


线程:
unit uMyThread;

interface

uses
  Classes;

type
  MyThread = class(TThread)
  private
  protected
  procedure Execute; override;
  end;

implementation

 

procedure MyThread.Execute;
var
  i:Integer;
begin
 inc(i)
end;

end.





[解决办法]
看你定义的,变量跟类都一样...

public
myThread:MyThread;
end;
[解决办法]
procedure MyThread.Execute;
var
i:Integer;
begin
 inc(i)
end;

这个一般没问题,不过还是得这样:
procedure MyThread.Execute;
var
i:Integer;
begin
 i:=0;
 inc(i)
end;

热点排行