因為我才學線程,為什麼我作一個下麵這個線程不成功?
程序才執行 線程的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 如何作? 謝謝!
程序才執行 線程的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 到底有什麼區別呢?
{主线程源代码,窗体上摆了一个Button(名称ButtonStart)/Edit(名称EditFlag)}
unit MainFormUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TMainForm = class(TForm)
ButtonStart: TButton;
EditFlag: TEdit;
procedure ButtonStartClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
MainForm: TMainForm;implementationuses MyThreadUnit;{$R *.dfm}procedure TMainForm.ButtonStartClick(Sender: TObject);
var
iThread:TMyThread;
begin
iThread := TMyThread.Create(true); //创建线程之后是否挂起,false为不挂起,直接运行线程;true为挂起,需要用resume方法唤醒线程
iThread.Resume;
end;end.{线程对象源代码}
unit MyThreadUnit;interfaceuses
Classes,Windows; //注意包含Windows单元,因为用到了API:Sleep()type
TMyThread = class(TThread)
private
procedure UpdateMainText;
protected
procedure Execute; override;
end;implementationuses MainFormUnit;procedure TMyThread.Execute;
var
i:Integer; //计数器
begin
FreeOnTerminate := true; //线程结束后自动删除
i := 0;
while i< 100 do
begin
Sleep(100);
inc(i);
end; //那么这个过程需要10秒钟才会结束 Synchronize(UpdateMainText); //凡是访问线程外其它VCL的属性,都必须使用这个过程来同步,否则可能会出问题
end;procedure TMyThread.UpdateMainText;
begin
MainForm.EditFlag.Text := '线程已完成!';
end;end.
你通过Delphi的向导创建了一个线程对象之后,
会在生成的代码上面有这么一段话,请注意看:
{ 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 asd.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ asd }即是说明了如何在线程中调用其它VCL控件的属性,以及如何与他们同步 。
应该这样写:TTest:=TMYThread.create;
当你动态度创建一个窗体时,你怎么写的代码?一样呀,
var
form1:Tform;
你不会这样写吧:form1.create()?而是这样写的吧:form1:=Tform.create(self);
兩位大哥:
我用TTest:=TMYThread.create; 後就成功了,可什麼時候用
TTest:=TMYThread.create; 什麼時候用:
TTest.create; ?
1、我的窗體是自動建立的,請詳細講一下原因
2、我發現有的例子在 線程的 Create後加了一個 virtual;,有什麼實際意義?