因為我才學線程,為什麼我作一個下麵這個線程不成功?
程序才執行 線程的Create就報內存出錯!/////////////////// 線程代碼
unit MyThread;interfaceuses
Classes;type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
Public
constructor Create;
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TMyThread }constructor TMyThread.Create;
begin
inherited create(true);
end;procedure TMyThread.Execute;
var
i:integer;
begin
{ Place thread code here }
i:=0;
end;end.
////////////////// 主程序代碼
unit Main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TfmMain = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
fmMain: TfmMain;implementation{$R *.dfm}uses
MyThread;
var
TTest:TMyThread;procedure TfmMain.Button1Click(Sender: TObject);
begin
TTest.Create;
end;end.---------------------------
1、請分析出錯原因。
2、如何解決?
3、如果要在線程裏引用VCL如 adoDataset, edit 如何作? 謝謝!
4:
在主程序開頭中的 uses 與 程序中間的 uses 到底有什麼區別呢?
程序才執行 線程的Create就報內存出錯!/////////////////// 線程代碼
unit MyThread;interfaceuses
Classes;type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
Public
constructor Create;
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TMyThread }constructor TMyThread.Create;
begin
inherited create(true);
end;procedure TMyThread.Execute;
var
i:integer;
begin
{ Place thread code here }
i:=0;
end;end.
////////////////// 主程序代碼
unit Main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TfmMain = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
fmMain: TfmMain;implementation{$R *.dfm}uses
MyThread;
var
TTest:TMyThread;procedure TfmMain.Button1Click(Sender: TObject);
begin
TTest.Create;
end;end.---------------------------
1、請分析出錯原因。
2、如何解決?
3、如果要在線程裏引用VCL如 adoDataset, edit 如何作? 謝謝!
4:
在主程序開頭中的 uses 與 程序中間的 uses 到底有什麼區別呢?
TTest:=TMYTherad.create;
是的,我按你們的說的作對了,可以詳細講一下為什麼不能用 TTest.create; 嗎?
通过对TTest进行赋值使它指向一个真实的对象,然后才能引用它。