100分求一菜鸟例程;单线程在Memo中增加100行内容。顶者有分。最迟本周日结贴。

xzh65414 2004-11-23 05:44:51
为了学习线程,特出此贴。
我找过了多线程中的所有贴子。然后自已又试了很多次,但总是有很多问题。
现求一程序功能如下:
1、采用线程;
2、在Memo中增加100行。内容随便。
请各位高手帮忙。感谢!
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzh65414 2004-11-23
  • 打赏
  • 举报
回复
谢谢 bee2518(迷茫ing) , aiirii(ari-爱的眼睛)
结帖。
bee2518 2004-11-23
  • 打赏
  • 举报
回复
写的很辛苦,满足你的要求
bee2518 2004-11-23
  • 打赏
  • 举报
回复
unit Unit1;

interface

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

type
TMyThread = class(TThread)
private
FMemo:TMemo;
protected
procedure Execute; override;
public
constructor Create(Memo:TMemo);
destructor Destroy; override;
end;

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

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
MyThread :TMyThread;
begin
MyThread := TMyThread.Create(Memo1);
end;

{ TMyThread }

constructor TMyThread.Create(Memo: TMemo);
begin
FMemo := Memo;
FreeOnTerminate := False;
inherited Create(False);
end;

destructor TMyThread.Destroy;
begin

inherited;
end;

procedure TMyThread.Execute;
var
i:integer;
begin
inherited;
for i:=1 to 100 do
FMemo.Lines.Add('Lines'+IntToStr(i));
end;

end.
xzh65414 2004-11-23
  • 打赏
  • 举报
回复
我很菜的。
下面有一段,但出错。
帮看看。
unit Unit2;
interface
uses
Classes,SysUtils;
type
MyThread = class(TThread)
private
{ Private declarations }
procedure AddLineToMemo;
protected
procedure Execute; override;

end;

implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure MyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ MyThread }

procedure MyThread.AddLineToMemo;
begin
form1.memo1.lines.add('=============');
end;

procedure MyThread.Execute;
var
i :integer;
begin
{ Place thread code here }
for i := 1 to 100 do
Synchronize(AddLineToMemo);

end;

end.







在单击事件中有。
var
mythread1:MyThread;
begin
mythread1.create(true);
mythread1.FreeOnTerminate:=true;
mythread1.resume;
end;


那个mythread1.resume;的时候出错了。什么内存地址不应该为xxxxxx.
编译时有一warning.说mythread1没有初始化。
还是没有成功。请高手再次,赐教。感谢。
aiirii 2004-11-23
  • 打赏
  • 举报
回复
C:\Program Files\Borland\Delphi7\Demos\Threads

delphi 帶的 demo 中應該就是你要的

16,742

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧