关于多线程的问题 ( 积分: 30 )

  • 主题发起人 主题发起人 delphijob
  • 开始时间 开始时间
D

delphijob

Unregistered / Unconfirmed
GUEST, unregistred user!
unit2的代码如下:
unit Unit2;
interface
uses
Classes,Windows,Dialogs,SysUtils;
type
mythread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
uses unit1;
procedure mythread.Execute;
begin
{ Place thread code here }
exit;
end;
////////////////////////
unit3的代码如下:
unit Unit3;
interface
uses
Classes,Windows,Dialogs,SysUtils;
type
restore_thread= class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
uses unit1;
procedure restore_thread.Execute;
begin
{ Place thread code here }
exit;
end;
////////////////////////////
unit1中的代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, DB, ADODB,IniFiles, ComCtrls;
var
Form1: TForm1;
implementation
uses unit2,unit3;
var
thread1:mythread;
thread2:restore_thread;
procedure TForm1.Button1Click(Sender: TObject);
begin
thread2.Create(false);
thread1.Create(false);
end;
可是在form1里点击按扭的时候就报错了:
access voilation at address 00423160 in module 'project2.exe'.write or address OOOOOOOE
知道原因的朋友们请指点指点啊!
 
thread1 := mythread.create(false);
2同
 
你的代码书写有误,呵呵
{unit Unit1;

interface

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

type TForm1= class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;

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


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

var
Form1: TForm1;

implementation

var
thread1:mythread;
thread2:restore_thread;


procedure mythread.Execute;
begin
exit;
end;

procedure restore_thread.Execute;
begin
exit;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
thread2.Create(false);
thread1.Create(false);
end;

end.
}
unit Unit1;

interface

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

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


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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

var
thread1:mythread;
thread2:restore_thread;

procedure mythread.Execute;
begin
exit;
end;

procedure restore_thread.Execute;
begin
exit;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
thread1:= mythread.Create(false);
thread1.Execute;
thread2:= restore_thread.Create(false);
thread2.Execute;
end;

end.
 
这个和线程无关,是object pascal基础语法没搞清楚,Create是类方法,不是对象方法。
 
楼上说的对
 
谢谢!!
我想问一下为什么在线程函数中加入showmessage()函数会报错呢?
procedure restore_thread.Execute;
begin
{ Place thread code here }
showmessage('你好!');//此处加上
end;
 
后退
顶部