一个已经调试通过的线程软件,但是我心不很底.帮我看看程序有没有潜在的问题.不懂线程的是学习的好机会

tonycjl 2007-02-02 04:44:19
unit Unit1;

interface

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

type
TThdMsg = Packed Record
MsgStr :Array[0..100] of Char;
TitStr :Array[0..100] of Char;
HShEdit :Thandle;

end;
PThdMsg = ^TThdMsg;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

Procedure ThreadPro(p_ThdMsg:PThdMsg);stdcall;
var
ThdMsg :TThdMsg;
TmpIdx :Smallint;
begin
ThdMsg :=p_ThdMsg^;
//Messagebox(0,@ThdMsg.MsgStr,@ThdMsg.TitStr,64);
SetWindowText(ThdMsg.HShEdit,'');
for TmpIdx :=0 to 1230 do
begin
SetWindowText(ThdMsg.HShEdit,Pchar(ThdMsg.MsgStr+' '+IntToStr(TmpIDX)));
SLEEP(1);
{^^^^^^^
CPU分享,如果需要时间更小,怎么样做呢。
如果不调这个.application.ProcessMessages ;
}
end;
ThdMsg.MsgStr :='';
SetWindowText(ThdMsg.HShEdit,pchar(INTTOSTR(ThdMsg.HShEdit)+','+datetimetoStr(now)));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Thd1Msg,Thd2Msg :TThdmsg;
Thd3Msg,Thd4Msg :TThdmsg;
Thd5Msg,Thd6Msg :TThdmsg;
Thd7Msg,Thd8Msg :TThdmsg;
ThdID :Longword;
begin
Edit1.Clear ;
strcopy(@Thd1Msg.MsgStr,'This is Messagebox String A');
Strcopy(@Thd1Msg.TitStr,'This is Messagebox Caption A');
Thd1Msg.HShEdit :=Edit1.Handle ;
CreateThread(nil,0,@ThreadPro,@Thd1Msg,0,ThdID);
// sleep(1);
//application.ProcessMessages ;
Edit2.Clear ;
strcopy(@Thd2Msg.MsgStr,'This is Messagebox String B');
Strcopy(@Thd2Msg.TitStr,'This is Messagebox Caption B');
Thd2Msg.HShEdit :=Edit2.Handle ;
CreateThread(nil,0,@ThreadPro,@Thd2Msg,0,ThdID);
// sleep(1);
//application.ProcessMessages ;
Edit3.Clear ;
strcopy(@Thd3Msg.MsgStr,'This is Messagebox String C');
Strcopy(@Thd3Msg.TitStr,'This is Messagebox Caption C');
Thd3Msg.HShEdit :=Edit3.Handle ;
CreateThread(nil,0,@ThreadPro,@Thd3Msg,0,ThdID);
// sleep(1);
//application.ProcessMessages ;
Edit4.Clear ;
strcopy(@Thd4Msg.MsgStr,'This is Messagebox String D');
Strcopy(@Thd4Msg.TitStr,'This is Messagebox Caption D');
Thd4Msg.HShEdit :=Edit4.Handle ;
CreateThread(nil,0,@ThreadPro,@Thd4Msg,0,ThdID);
// sleep(1);
//application.ProcessMessages ;
Edit5.Clear ;
strcopy(@Thd5Msg.MsgStr,'This is Messagebox String E');
Strcopy(@Thd5Msg.TitStr,'This is Messagebox Caption E');
Thd5Msg.HShEdit :=Edit5.Handle ;
CreateThread(nil,0,@ThreadPro,@Thd5Msg,0,ThdID);
sleep(1);
//application.ProcessMessages ;
Edit6.Clear ;
strcopy(@Thd6Msg.MsgStr,'This is Messagebox String F');
Strcopy(@Thd6Msg.TitStr,'This is Messagebox Caption F');
Thd6Msg.HShEdit :=Edit6.Handle ;
CreateThread(nil,0,@ThreadPro,@Thd6Msg,0,ThdID);
// sleep(1);
//application.ProcessMessages ;
Edit7.Clear ;
strcopy(@Thd7Msg.MsgStr,'This is Messagebox String G');
Strcopy(@Thd7Msg.TitStr,'This is Messagebox Caption G');
Thd7Msg.HShEdit :=Edit7.Handle ;
CreateThread(nil,0,@ThreadPro,@Thd7Msg,0,ThdID);
sleep(1);
//application.ProcessMessages ;
Edit8.Clear ;
strcopy(@Thd8Msg.MsgStr,'This is Messagebox String H');
Strcopy(@Thd8Msg.TitStr,'This is Messagebox Caption H');
Thd8Msg.HShEdit :=Edit8.Handle ;
CreateThread(nil,0,@ThreadPro,@Thd8Msg,0,ThdID);
sleep(10000);
//application.ProcessMessages ;
end;

end.
...全文
218 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonycjl 2007-02-07
  • 打赏
  • 举报
回复
这个是测试程序,我一个用程序中,我要用同一个函数用多个线程处理多个不同的参数的.我要同时控制很多台机,但它们的功能不同
何鲁青 2007-02-02
  • 打赏
  • 举报
回复
没看懂什么意思,不知道为什么不用TThread

16,748

社区成员

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

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