请熟手帮忙看看吧,我写的例子运行起来怎么还是单线程的效果?当我按不同的按钮应该能分别启动不同的线程才对的吧!谢谢了,虽然不早,我还会一直在线等候的。 ( 积分:

  • 主题发起人 主题发起人 xiaodp
  • 开始时间 开始时间
X

xiaodp

Unregistered / Unconfirmed
GUEST, unregistred user!
请熟手帮忙看看吧,我写的例子运行起来怎么还是单线程的效果?当我按不同的按钮应该能分别启动不同的线程才对的吧!谢谢了,虽然不早,我还会一直在线等候的。 ( 积分: 100 )<br />如题,代码如下:帮忙看看错在哪儿?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Db, ADODB,DosMove, ComCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TMyThread = class(TThread)
MyMemo : TMemo;
procedure show;virtual;abstract;
constructor Create(MyMemo1: TMemo);
end;

Thread1 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
Thread2 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
Thread3 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;

var
Form1: TForm1;
conString : String;
implementation
{$R *.dfm}
constructor TMyThread.create(MyMemo1:TMemo);
begin
inherited create(False);
MyMemo := MyMemo1;
FreeOnTerminate := true;
end;

procedure Thread1.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i));
end;

procedure Thread2.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i*2));
end;

procedure Thread3.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i*i));
end;

procedure Thread1.Execute;
begin
Synchronize(show);
end;

procedure Thread2.Execute;
begin
Synchronize(show);
end;

procedure Thread3.Execute;
begin
Synchronize(show);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
with Thread1.Create(Memo1)do
;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Memo2.Clear;
with Thread2.Create(Memo2)do
;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
Memo3.Clear;
with Thread3.Create(Memo3)do
;
end;

end.
 
如题,代码如下:帮忙看看错在哪儿?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Db, ADODB,DosMove, ComCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TMyThread = class(TThread)
MyMemo : TMemo;
procedure show;virtual;abstract;
constructor Create(MyMemo1: TMemo);
end;

Thread1 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
Thread2 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;
Thread3 = class(TMyThread)
procedure show;override;
procedure Execute;override;
end;

var
Form1: TForm1;
conString : String;
implementation
{$R *.dfm}
constructor TMyThread.create(MyMemo1:TMemo);
begin
inherited create(False);
MyMemo := MyMemo1;
FreeOnTerminate := true;
end;

procedure Thread1.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i));
end;

procedure Thread2.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i*2));
end;

procedure Thread3.show ;
var
i:integer;
begin
for i := 0 to 1000do
MyMemo.Lines.Add(intToStr(i*i));
end;

procedure Thread1.Execute;
begin
Synchronize(show);
end;

procedure Thread2.Execute;
begin
Synchronize(show);
end;

procedure Thread3.Execute;
begin
Synchronize(show);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
with Thread1.Create(Memo1)do
;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Memo2.Clear;
with Thread2.Create(Memo2)do
;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
Memo3.Clear;
with Thread3.Create(Memo3)do
;
end;

end.
 
Synchronize(show);本来就是在主线程中执行的,所以你看起来是单线程效果。
 
又见东兰, [:D]
 
麻子MM,来亲亲……
 
时间不早了,不要等了,快去睡吧!
 
东兰, 偶只给MM亲的~ [:D]
 
请哪位大虾给我改一下怎样启动多个线程吧!如果100觉得少,我再开贴给你一100分,初学线程,真是啥也搞不明白了,痛苦中……ing!
 
就真的没人帮我?自己顶顶吧!
 
你要多线程访问VCL,会有不确定问题的。
 
说得对
如果要实现你想像中的效果,你该这么做
申请一块足够大的内存,用于保存Memo中的内容(或用链表等动态结构)
两个线程互斥地访问该结构,向其中添加内容,最好同时维护一个全局变量以使线程内更容易配合
主线程(也可能是两个线程中的一个)读取该结构数据,并在其内容有改动时添加到memo中
这样做是另外的线程不直接访问VCL,绕开了多线VCL操作
 
procedure Thread1.show ;
var
i:integer;
begin
for i := 0 to 1000do
begin
MyMemo.Lines.Add(intToStr(i));
Application.ProcessMessages;
end;
end;

procedure Thread2.show ;
var
i:integer;
begin
for i := 0 to 1000do
begin
MyMemo.Lines.Add(intToStr(i*2));
Application.ProcessMessages;
end;
end;

procedure Thread3.show ;
var
i:integer;
begin
for i := 0 to 1000do
begin
MyMemo.Lines.Add(intToStr(i*i));
Application.ProcessMessages;
end;
end;

procedure Thread1.Execute;
begin
show;
end;

procedure Thread2.Execute;
begin
show;
end;

procedure Thread3.Execute;
begin
show;
end;
 
现在多线程可以实现了,但是在多线程中如果我用ADO访问数据库时,就会出现没有调用CoInitinalize的错误提示,这是什么原因呢?
 
ADO
是Activex控件,需要初始化库
 
在线程入口调用CoInitialize(Nil);
出口处相应的CoUninitialize;
 
to someset :
CoInitialize(Nil) 应加在什么位置?我加入后提示没申明该函数呀!
 
后退
顶部