说明:把多个数据分别传到多个线程中,出错了。例如:现有一个字符串数组,里面有三个字符串,这是我要创建3个线程,分别处理这个字符串。但是在我写的代码中,所有的线程只处理了字符串数组中的最后一个字符串。为什么呀????
主要是下面的 for 循环和ThreadCopyAddress.Execute过程代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
ListBox2: TListBox;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
OldSL: TStringList;
CopyEnd: Boolean;
S: String;
end;var
Form1: TForm1;implementation
uses Unit2;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
CopyAddress,CopyAddress1:ThreadCopyAddress;
i: Integer;
begin
OldSL := TStringList.Create; OldSL.Assign(ListBox1.Items); for i:=0 to OldSL.Count-1 do
begin
S := Form1.OldSL.Strings[i];
CopyAddress := ThreadCopyAddress.Create(false);
end;
end;
unit Unit2;interfaceuses
Classes;type
ThreadCopyAddress = class(TThread)
private
{ Private declarations }
procedure ClearOldSL;
protected
procedure Execute; override;
end;implementation{ ThreadCopyAddress }
uses Unit1;
procedure ThreadCopyAddress.ClearOldSL;
begin
Form1.OldSL.Clear;end;procedure ThreadCopyAddress.Execute;
var
i: Integer;
begin
{ Place thread code here } Form1.ListBox2.Items.Add(Form1.S);
Synchronize(ClearOldSL);
end;end.
主要是下面的 for 循环和ThreadCopyAddress.Execute过程代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
ListBox2: TListBox;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
OldSL: TStringList;
CopyEnd: Boolean;
S: String;
end;var
Form1: TForm1;implementation
uses Unit2;
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
CopyAddress,CopyAddress1:ThreadCopyAddress;
i: Integer;
begin
OldSL := TStringList.Create; OldSL.Assign(ListBox1.Items); for i:=0 to OldSL.Count-1 do
begin
S := Form1.OldSL.Strings[i];
CopyAddress := ThreadCopyAddress.Create(false);
end;
end;
unit Unit2;interfaceuses
Classes;type
ThreadCopyAddress = class(TThread)
private
{ Private declarations }
procedure ClearOldSL;
protected
procedure Execute; override;
end;implementation{ ThreadCopyAddress }
uses Unit1;
procedure ThreadCopyAddress.ClearOldSL;
begin
Form1.OldSL.Clear;end;procedure ThreadCopyAddress.Execute;
var
i: Integer;
begin
{ Place thread code here } Form1.ListBox2.Items.Add(Form1.S);
Synchronize(ClearOldSL);
end;end.
解决方案 »
- 怎么取到另一程序弹出消息框中的提示信息的文本?
- 请教用一个Function得到表的逻辑(中文)字段名
- 如何检查query查询出来的记录是否为空?谢谢!
- 请问哪里有DELPHI网络控件编程的资料?谢谢
- 请问怎样在quickreport打印数据时加上表格?谢谢
- 如何从若干个图片中搜索出来包含指定部分内容的图片?
- 【200分紧急求助】”用QR的ON NEEDDATA、打印内容所在BAND的AFTER PRINT事件中进行判断,可控制打印行数、不足一页用空行填满。“这个
- 关于GetSystemMetrics函数。
- 大家来讨论一下!关于异常的问题!来者有分!
- 有谁知道在SQLServer7中,如何查看用户的详细操作日志。。。如:Insert,select等操作。一旦解决马上给分,谢谢
- 问一个关于语法分析的问题,就是编译器如何将我们写的代码分解成可执行的最小单元。
- 高分求解:dll问题!?
procedure ThreadCopyAddress.ClearOldSL;
begin
Form1.OldSL.Clear;end;procedure ThreadCopyAddress.Execute;
var
i: Integer;
begin
{ Place thread code here } Form1.ListBox2.Items.Add(Form1.S);
Synchronize(ClearOldSL);
end;这段代码明显有问题,每个线程当然只处理了一个相同的S啊! Synchronize(ClearOldSL);
这句有问题
修改为:
Form1.ListBox2.Items.Add(Form1.S);
sleep(1000); // 可适当减小这个值
Synchronize(ClearOldSL);
假如用 sleep(1000)话,这样性能就很差了?这个方法不可取
......procedure TForm1.Button1Click(Sender: TObject);
var
CopyAddress,CopyAddress1:ThreadCopyAddress;
i: Integer;
begin
OldSL := TStringList.Create;
OldSL.Assign(ListBox1.Items); for i:=0 to OldSL.Count-1 do
begin
S := Form1.OldSL.Strings[i];
CopyAddress := ThreadCopyAddress.Create(TRUE);
CopyAddress.InStr := S;
CopyAddress.Resume();
end;
end;
unit Unit2;interfaceuses
Classes;type
ThreadCopyAddress = class(TThread)
private
{ Private declarations }
procedure ClearOldSL;
protected
procedure Execute; override;
public
InStr:string;
end;implementation uses Unit1;{ 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 hreadCopyAddress.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ hreadCopyAddress }procedure ThreadCopyAddress.ClearOldSL;
begin
Form1.ListBox2.Items.Add(InStr);
end;procedure ThreadCopyAddress.Execute;
begin
{ Place thread code here }
Synchronize(ClearOldSL);
end;end.