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