请教多线程问题,我增加了两个测试项目,两个测试分别创建2个线程执行,结果只有后面的线程执行,什么原因 ( 积分: 100 )

L

lhjiang

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下:addtest---这个是增加测试,开启一个线程
unit addtest;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls, Grids, DBGrids, Spin, Ping, DB,
DBTables, DBGridEh,WinSock,DateUtils;
type
TForm_addtest = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
test_name: TEdit;
Label4: TLabel;
url_memo: TEdit;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label5: TLabel;
ComboBox2: TComboBox;
Label6: TLabel;
ComboBox3: TComboBox;
Label7: TLabel;
Label8: TLabel;
ComboBox4: TComboBox;
Label9: TLabel;
ComboBox_year: TComboBox;
Label10: TLabel;
ComboBox_month: TComboBox;
Label11: TLabel;
ComboBox_day: TComboBox;
Label12: TLabel;
GroupBox2: TGroupBox;
Label13: TLabel;
ComboBox5: TComboBox;
Label19: TLabel;
url_related: TEdit;
Button1: TButton;
OpenDialog1: TOpenDialog;
GroupBox3: TGroupBox;
GroupBox4: TGroupBox;
Label20: TLabel;
ComboBox_depend: TComboBox;
Panel1: TPanel;
PageControl2: TPageControl;
ping_sheet: TTabSheet;
Label21: TLabel;
Label22: TLabel;
Label23: TLabel;
Label24: TLabel;
Label25: TLabel;
Label26: TLabel;
ping_addr: TEdit;
ping_timeout: TEdit;
ping_size: TEdit;
ping_nums: TEdit;
ping_deadline: TSpinEdit;
url_sheet: TTabSheet;
Label27: TLabel;
url_address: TEdit;
Label28: TLabel;
url_timeout: TSpinEdit;
Label29: TLabel;
Button2: TButton;
Label30: TLabel;
ComboBox7: TComboBox;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Ping1: TPing;
Label2: TLabel;
ComboBox_enable: TComboBox;
test_id: TEdit;
DataSource1: TDataSource;
Query1: TQuery;
ComboBox_hour: TComboBox;
Label3: TLabel;
ComboBox_min: TComboBox;
Label14: TLabel;
DBGridEh1: TDBGridEh;
DBGridEh2: TDBGridEh;
DBGridEh3: TDBGridEh;
ComboBox_alert: TComboBox;
Timer1: TTimer;
pagecontrol1: TPageControl;
procedure ComboBox7Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button4Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;
{
type
tmythread = class(Tthread)
private
ptime:string;
sleeptime:integer;
function PingHost(HostIP: string): Boolean;
procedure OutputRes;
protected
procedure execute;
override ;
procedure giveanswer;
public
// procedure PingHost;
end;
}
var
Form_addtest: TForm_addtest;
test_status:string;
test_id:string;
var
exc:boolean;
i:integer;
pingtime:string;
implementation
uses frm_add_advance, dbmodule, maifrm, testthread, main;
{$R *.dfm}
procedure TForm_addtest.ComboBox7Change(Sender: TObject);
begin
pagecontrol2.ActivePageIndex:=combobox7.ItemIndex;
end;

procedure TForm_addtest.Button2Click(Sender: TObject);
begin
fm_addtest_advance.Show;
end;

procedure TForm_addtest.Button3Click(Sender: TObject);
var
hh:integer;
mm:integer;
temp:integer;
temptime:Ttime;
begin

// showmessage(inttostr(strtoint(combobox2.Text)-strtoint(formatdatetime('hh',now))*3600000));
temp:=(strtoint(combobox2.Text)-strtoint(formatdatetime('hh',now)))*3600000+(strtoint(combobox3.Text)-1-strtoint(formatdatetime('mm',now)))*60000+(60-strtoint(formatdatetime('ss',now)))*1000;
temptime:=strtotime(combobox2.Text+':'+combobox3.Text+':00');
showmessage(inttostr(secondsbetween(temptime,now)));
mainfrm.Edit_ip.Text:=ping_addr.Text;
mainfrm.Edit_pingtime.Text:=inttostr(temp);
testthread.tmythread.Create(false);
// Ping1.DnsLookup(ping_addr.Text);
// ping1.Address:='178.100.101.226';
// Ping1.Ping;
datamodule1.Query1.Close;
datamodule1.Query1.SQL.Clear;
//插入test_id_tbl表
datamodule1.Query1.SQL.Add('insert into test_id_tbl values(seq_testid.nextval,:test_name,:test_type,:memo,:related_url,:regular_id,:alert_status,:alert_action,:log_mod,:common_log,:enabled,sysdate,sysdate,:test_depend,:test_depend_id)');
datamodule1.Query1.ParamByName('test_name').Value:=test_name.Text;
datamodule1.Query1.ParamByName('test_type').Value:=combobox7.Text;
datamodule1.Query1.ParamByName('memo').Value:=url_memo.Text;
datamodule1.Query1.ParamByName('related_url').Value:=url_related.Text;
datamodule1.Query1.ParamByName('regular_id').Value:=pagecontrol1.ActivePage.Caption;
datamodule1.Query1.ParamByName('alert_status').Value:=combobox_alert.ItemIndex;
datamodule1.Query1.ParamByName('alert_action').Value:=combobox5.Text;
datamodule1.Query1.ParamByName('log_mod').Value:=combobox7.Text;
datamodule1.Query1.ParamByName('common_log').Value:='';
datamodule1.Query1.ParamByName('enabled').Value:=combobox_enable.Text;
// datamodule1.Query1.ParamByName('create_time').AsDate:=to_date('''+FromatdateTime('YYYY-MM-DD',NOW)+''',''yyyy-mm-dd'');
// datamodule1.Query1.ParamByName('modify_time').AsDate:=to_date('''+FromatdateTime('YYYY-MM-DD',NOW)+''',''yyyy-mm-dd'');
//是否有测试依赖
if combobox_depend.
Text <> '' then
begin
datamodule1.Query1.ParamByName('test_depend_id').Value:=combobox_depend.
Text;
datamodule1.Query1.ParamByName('test_depend').Value:='true';
end
else
begin
datamodule1.Query1.ParamByName('test_depend').Value:='false';
datamodule1.Query1.ParamByName('test_depend_id').value:='';
end;

datamodule1.Query1.ExecSQL;
datamodule1.Query1.Close;
//获得test_id
{
datamodule1.Query2.Close;
datamodule1.Query2.Database.Connected:=false;
datamodule1.Query2.Database.Connected:=True;
datamodule1.Query2.SQL.Clear;
datamodule1.Query2.SQL.Add('select max(test_id) from test_id_tbl');
datamodule1.Query2.Open;
if datamodule1.Query2.RecordCount>0 then
begin
test_id:=datamodule1.Query2.DataSource.DataSet.Fields.Fields[0].AsString;
end;
}
{ //插入结果表test_result_tbl
datamodule1.Query1.Close;
datamodule1.Query1.SQL.Clear;
datamodule1.Query1.SQL.Add('insert into test_result_tbl values(seq_testid.CURRVAL,:test_status,:test_reply,:test_method,:alive,:dead,:unknown,:total_time,:alive_time,:dead_time,:tests,:passed_tests,:dead_tests,:average_time,:min_time,:max_time,sysdate,sysdate,sysdate)');
//datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_status').Value:=test_status;
datamodule1.Query1.ParamByName('test_reply').Value:=ping1.Reply.RTT;
datamodule1.Query1.ParamByName('test_method').Value:=Combobox7.Text;
datamodule1.Query1.ParamByName('alive').Value:=100;
datamodule1.Query1.ParamByName('dead').Value:=0;
datamodule1.Query1.ParamByName('unknown').Value:=0;
datamodule1.Query1.ParamByName('total_time').Value:=1;
datamodule1.Query1.ParamByName('alive_time').Value:=1;
datamodule1.Query1.ParamByName('dead_time').Value:=0;
datamodule1.Query1.ParamByName('tests').Value:=1;
datamodule1.Query1.ParamByName('passed_tests').Value:=1;
datamodule1.Query1.ParamByName('dead_tests').Value:=1;
datamodule1.Query1.ParamByName('average_time').Value:=0;
datamodule1.Query1.ParamByName('min_time').Value:=1;
datamodule1.Query1.ParamByName('max_time').Value:=1;
// datamodule1.Query1.ParamByName('change_time').Value:='';
// datamodule1.Query1.ParamByName('start_time').Value:='';
// datamodule1.Query1.ParamByName('end_time').Value:='';
datamodule1.Query1.ExecSQL;
}
//插入ping_properties_tbl表
if ( combobox7.ItemIndex = 0 ) then
begin
datamodule1.Query1.Close;
datamodule1.query1.sql.clear;
datamodule1.Query1.SQL.Add('insert into ping_properties_tbl values(seq_testid.CURRVAL,:test_url,:timeout,:packet_size,:packets,:deadline)');
// datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_url').Value:=ping_addr.Text;
datamodule1.Query1.ParamByName('timeout').Value:=ping_timeout.Text;
datamodule1.Query1.ParamByName('packet_size').Value:=ping_size.Text;
datamodule1.Query1.ParamByName('packets').Value:=ping_nums.Text;
datamodule1.Query1.ParamByName('deadline').Value:=ping_deadline.Text;
datamodule1.Query1.ExecSQL;
// datamodule1.Query1.Close;
end
else
//否则插入url_properties_tbl表
begin
datamodule1.Query1.Close;
datamodule1.query1.sql.clear;
datamodule1.Query1.SQL.Add('insert into url_properties_tbl values(seq_testid.CURRVAL,:test_url,:timeout,:advanced_set)');
//datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_url').Value:=url_address.Text;
datamodule1.Query1.ParamByName('timeout').Value:=url_timeout.Text;
datamodule1.Query1.ParamByName('advanced_set').Value:='false';
datamodule1.Query1.ExecSQL;
// datamodule1.Query1.Close;
end;
//是常规日程
if pagecontrol1.ActivePageIndex=0 then
begin
datamodule1.Query1.Close;
datamodule1.query1.sql.clear;
datamodule1.Query1.SQL.Add('insert into regular_tbl values(seq_testid.CURRVAL,:regular_schedule,:hour,:minute)');
datamodule1.Query1.ParamByName('regular_schedule').Value:=combobox4.Text;
datamodule1.Query1.ParamByName('hour').Value:=combobox2.Text;
datamodule1.Query1.ParamByName('minute').Value:=combobox3.Text;
datamodule1.Query1.ExecSQL;
end
else
//非常规日程
begin
datamodule1.Query1.Close;
datamodule1.query1.sql.clear;
datamodule1.Query1.SQL.Add('insert into irregular_tbl values(seq_testid.CURRVAL,:year,:month,:day,:hour,:minute)');
datamodule1.Query1.ParamByName('year').Value:=combobox_year.Text;
datamodule1.Query1.ParamByName('month').Value:=combobox_month.Text;
datamodule1.Query1.ParamByName('day').Value:=combobox_day.Text;
datamodule1.Query1.ParamByName('hour').Value:=combobox_hour.Text;
datamodule1.Query1.ParamByName('minute').Value:=combobox_min.Text;
datamodule1.Query1.ExecSQL;
end;
form_addtest.Close;
mainfrm.DBGridEh1.DataSource.DataSet.Active:=false;
mainfrm.DBGridEh1.DataSource.DataSet.Active:=true;
//threed part
// exc:=true;
{
if pagecontrol1.ActivePage.Name='tabsheet1' then
begin
hh:=strtoint(combobox2.Text);
mm:=strtoint(combobox3.Text);
showmessage(inttostr((strtoint(formatdatetime('hh',now))-strtoint(combobox2.Text))*3600000+(strtoint(formatdatetime('mm',now))-strtoint(combobox3.Text))*60000+strtoint(formatdatetime('mm',now))*1000));
//showmessage(formatdatetime('hh:mm:ss',now)-formatdatetime('hh:mm:ss',now));
end;
}
// if combobox_enable.Text='Enabled' then
// tmythread.Create(false);
end;

{
procedure TForm_addtest.Ping1DnsLookupDone(Sender: TObject;
Error: Word);
begin
Ping1.Address := Ping1.DnsResult;
Ping1.Ping;
end;

procedure TForm_addtest.Ping1EchoReply(Sender, Icmp: TObject;
Status: Integer);
begin

if Status <> 0 then
begin
//url_memo.Text:='Received ' + IntToStr(Ping1.Reply.DataSize) +' bytes from ' + Ping1.HostIP +' in ' + IntToStr(Ping1.Reply.RTT) + ' msecs';
test_status:='ok';
url_memo.Text:='ok';
end
else

test_status:='bad';
// url_memo.Text:='Cannot ping host (' + Ping1.HostIP + ') : ' +Ping1.ErrorString +'. Status = ' + IntToStr(Ping1.Reply.Status);
url_memo.Text:='bad';
end;
}
procedure TForm_addtest.Button5Click(Sender: TObject);
begin
form_addtest.Close;
end;

procedure TForm_addtest.FormShow(Sender: TObject);
begin
{ Query1.Close;
query1.sql.clear;
Query1.SQL.Add('select * from test_id_tbl where test_id=:test_id');
Query1.ParamByName('test_id').Value:=test_id.Text;
Query1.Open;
// if not Query1.DataSource.DataSet.EOF then
// begin
// query1.DataSource.DataSet.First;
test_name.Text:=Query1.DataSource.DataSet.Fields.Fields[1].Value;
// end;
}
end;

procedure TForm_addtest.Timer1Timer(Sender: TObject);
begin
//在定时中触发线程ping执行
if formatdatetime('hh:mm:ss',now)='14:03:30' then
begin
exc:=true;
// tmythread.Create(false);
end;
end;

procedure TForm_addtest.Button4Click(Sender: TObject);
var dd,ddd:Ttime;
temp:string;
begin

ddd:=strtotime('02:02:02');
dd:=strtotime(combobox2.Text+':'+combobox3.Text+':00');
//showmessage(combobox2.Text+':'+combobox3.Text+':00');
showmessage(formatdatetime('hh:mm:ss',now));
// showmessage(timetostr(formatdatetime('hh:mm:ss',now)-dd));
// showmessage(inttostr(strtoint(formatdatetime('hh',now))-hh)*60*60+strtoint(formatdatetime('mm',now))-mm)*60));
showmessage(inttostr(secondsbetween(now,dd)));
// testthread.tmythread.Create(false);
{ tmythread.Create(false);
}
end;

end.

-----------
线程程序 testthread
unit testthread;
interface
uses
Messages,Dialogs, SysUtils, Variants,WinSock,Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};
{
type
test = class(TThread)
private
procedure SetName;
protected
procedure Execute;
override;
end;
}
type
tmythread = class(Tthread)
private
ptime:string;
sleeptime:integer;
function PingHost(HostIP: string): Boolean;
//function gettime(time:integer):Integer;
//procedure OutputRes;
protected
procedure execute;
override ;
procedure giveanswer;
public
// procedure PingHost;
end;
implementation
uses test2, dbmodule, addtest, maifrm;
{ 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 test.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{$IFDEF MSWINDOWS}
type
TThreadNameInfo = record
FType: LongWord;
// must be 0x1000
FName: PChar;
// pointer to name (in user address space)
FThreadID: LongWord;
// thread ID (-1 indicates caller thread)
FFlags: LongWord;
// reserved for future use, must be zero
end;
{$ENDIF}
{ test }
procedure tmythread.execute;
var temp:integer;
begin
freeonterminate:=true;
temp:=strtoint(mainfrm.Edit_pingtime.Text);
sleep(temp);
synchronize(giveanswer);

// form_addtest.Ping1.DnsLookup(form_addtest.ping_addr.Text);
// form_addtest.Ping1.Address:=form_addtest.Ping1.DnsResult;
// form_addtest.Ping1.Ping;
// showmessage(IntToStr(form_addtest.Ping1.Reply.Status));
// form_addtest.Ping1.Address:=form_addtest.Ping1.DnsResult;
// form_addtest.Ping1.Ping;
// form_addtest.url_related.Text :=form_addtest.url_related.Text+IntToStr(form_addtest.Ping1.Reply.Status);
// if formatdatetime('hh:mm:ss',now)='15:36:00' then
// begin
{
if PingHost(form_addtest.ping_addr.Text) then
form_addtest.url_related.Text:='pingtime:'+pingtime+'ms';
else
form_addtest.url_related.Text :='ping is bad';
}
end;

procedure tmythread.giveanswer;
begin
// form_addtest.Ping1.DnsLookup(form_addtest.ping_addr.Text);
// form_addtest.Ping1.Address:=form_addtest.ping_addr.Text;
// form_addtest.Ping1.Ping;
// form_addtest.url_related.Text:=form_addtest.url_related.Text+IntToStr(form_addtest.Ping1.Reply.Status);
if PingHost(form_addtest.ping_addr.Text) then
begin
form_addtest.url_related.Text:='pingtime:'+ptime+'ms';
showmessage(ptime);
datamodule1.Query1.Close;
datamodule1.Query1.SQL.Clear;
datamodule1.Query1.SQL.Add('insert into test_result_tbl values(seq_testid.nextval,:test_status,:test_reply,:test_method,:alive,:dead,:unknown,:total_time,:alive_time,:dead_time,:tests,:passed_tests,:dead_tests,:average_time,:min_time,:max_time,sysdate,sysdate,sysdate)');
//datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_status').Value:='ok';
datamodule1.Query1.ParamByName('test_reply').Value:=ptime;
datamodule1.Query1.ParamByName('test_method').Value:=form_addtest.Combobox7.Text;
datamodule1.Query1.ParamByName('alive').Value:=100;
datamodule1.Query1.ParamByName('dead').Value:=0;
datamodule1.Query1.ParamByName('unknown').Value:=0;
datamodule1.Query1.ParamByName('total_time').Value:=1;
datamodule1.Query1.ParamByName('alive_time').Value:=1;
datamodule1.Query1.ParamByName('dead_time').Value:=0;
datamodule1.Query1.ParamByName('tests').Value:=1;
datamodule1.Query1.ParamByName('passed_tests').Value:=1;
datamodule1.Query1.ParamByName('dead_tests').Value:=1;
datamodule1.Query1.ParamByName('average_time').Value:=0;
datamodule1.Query1.ParamByName('min_time').Value:=1;
datamodule1.Query1.ParamByName('max_time').Value:=1;
// datamodule1.Query1.ParamByName('change_time').Value:='';
// datamodule1.Query1.ParamByName('start_time').Value:='';
// datamodule1.Query1.ParamByName('end_time').Value:='';
datamodule1.Query1.ExecSQL;
datamodule1.Query1.Close;
end
else
begin
form_addtest.url_related.Text :='ping is bad';
datamodule1.Query1.Close;
datamodule1.Query1.SQL.Clear;
datamodule1.Query1.SQL.Add('insert into test_result_tbl values(seq_testid.nextval,:test_status,:test_reply,:test_method,:alive,:dead,:unknown,:total_time,:alive_time,:dead_time,:tests,:passed_tests,:dead_tests,:average_time,:min_time,:max_time,sysdate,sysdate,sysdate)');
//datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_status').Value:='bad';
datamodule1.Query1.ParamByName('test_reply').Value:='unavailable';
datamodule1.Query1.ParamByName('test_method').Value:=form_addtest.Combobox7.Text;
datamodule1.Query1.ParamByName('alive').Value:=100;
datamodule1.Query1.ParamByName('dead').Value:=0;
datamodule1.Query1.ParamByName('unknown').Value:=0;
datamodule1.Query1.ParamByName('total_time').Value:=1;
datamodule1.Query1.ParamByName('alive_time').Value:=1;
datamodule1.Query1.ParamByName('dead_time').Value:=0;
datamodule1.Query1.ParamByName('tests').Value:=1;
datamodule1.Query1.ParamByName('passed_tests').Value:=1;
datamodule1.Query1.ParamByName('dead_tests').Value:=1;
datamodule1.Query1.ParamByName('average_time').Value:=0;
datamodule1.Query1.ParamByName('min_time').Value:=1;
datamodule1.Query1.ParamByName('max_time').Value:=1;
// datamodule1.Query1.ParamByName('change_time').Value:='';
// datamodule1.Query1.ParamByName('start_time').Value:='';
// datamodule1.Query1.ParamByName('end_time').Value:='';
datamodule1.Query1.ExecSQL;
datamodule1.Query1.Close;
end;
end;
{
function tmythread.gettime(time:integer):Integer;
begin
Result:=time;
end;
}
function tmythread.PingHost(HostIP:string): Boolean;
type
PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation = packed record
TTL: Byte;
TOS: Byte;
Flags: Byte;
OptionsSize: Byte;
OptionsData: PChar;
end;

PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply = packed record
Address: DWORD;
Status: DWORD;
RTT: DWORD;
DataSize: Word;
Reserved: Word;
Data: Pointer;
Options: TIPOptionInformation;
end;
TIcmpCreateFile = function: THandle;
stdcall;
TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean;
stdcall;
TIcmpSendEcho = function(IcmpHandle:THandle;
DestinationAddress: DWORD;
RequestData: Pointer;
RequestSize: Word;
RequestOptions: PIPOptionInformation;
ReplyBuffer: Pointer;
ReplySize: DWord;
Timeout: DWord
): DWord;
stdcall;
var
hICMP : THandle;
hICMPdll : THandle;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle : TIcmpCloseHandle;
IcmpSendEcho : TIcmpSendEcho;
pIPE : PIcmpEchoReply;// ICMP Echo reply buffer
FIPAddress : DWORD;
FSize : DWORD;
FTimeOut : DWORD;
BufferSize : DWORD;
pReqData,pRevData:pChar;
MyString:string;
begin
Result := False;
hICMPdll := LoadLibrary('icmp.dll');
if hICMPdll = 0 then
exit;
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll,'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
if (hICMP = INVALID_HANDLE_VALUE) then
exit;
FIPAddress := inet_addr(PChar(HostIP));
MyString := 'Hello,World';
//send data buffer
pReqData := PChar(MyString);
FSize := 40;
//receive data buffer
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
GetMem(pRevData,FSize);
pIPE^.Data := pRevData;
FTimeOut := 4000;
try
Result := IcmpSendEcho(hICMP, FIPAddress, pReqData,
Length(MyString),nil,pIPE,BufferSize,FTimeOut)>0;
ptime:=IntToStr(pIPE^.RTT);

finally
IcmpCloseHandle(hICMP);
FreeLibrary(hICMPdll);
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;

end.
-------------
我这里创建一个测试后,创建一个线程,但并没有马上执行,而是通过sleep()来控制执行测试的时间,这个睡眠时间是通过addtest里面的temp传给mainfrm的一个edit_pingtime得到的,我增加了2个测试,分别在5分钟,和6分钟后执行测试,结果只有后面一个测试执行了。请教我的问题在哪里?应该如何改进??谢谢
 
代码如下:addtest---这个是增加测试,开启一个线程
unit addtest;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls, Grids, DBGrids, Spin, Ping, DB,
DBTables, DBGridEh,WinSock,DateUtils;
type
TForm_addtest = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
test_name: TEdit;
Label4: TLabel;
url_memo: TEdit;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label5: TLabel;
ComboBox2: TComboBox;
Label6: TLabel;
ComboBox3: TComboBox;
Label7: TLabel;
Label8: TLabel;
ComboBox4: TComboBox;
Label9: TLabel;
ComboBox_year: TComboBox;
Label10: TLabel;
ComboBox_month: TComboBox;
Label11: TLabel;
ComboBox_day: TComboBox;
Label12: TLabel;
GroupBox2: TGroupBox;
Label13: TLabel;
ComboBox5: TComboBox;
Label19: TLabel;
url_related: TEdit;
Button1: TButton;
OpenDialog1: TOpenDialog;
GroupBox3: TGroupBox;
GroupBox4: TGroupBox;
Label20: TLabel;
ComboBox_depend: TComboBox;
Panel1: TPanel;
PageControl2: TPageControl;
ping_sheet: TTabSheet;
Label21: TLabel;
Label22: TLabel;
Label23: TLabel;
Label24: TLabel;
Label25: TLabel;
Label26: TLabel;
ping_addr: TEdit;
ping_timeout: TEdit;
ping_size: TEdit;
ping_nums: TEdit;
ping_deadline: TSpinEdit;
url_sheet: TTabSheet;
Label27: TLabel;
url_address: TEdit;
Label28: TLabel;
url_timeout: TSpinEdit;
Label29: TLabel;
Button2: TButton;
Label30: TLabel;
ComboBox7: TComboBox;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Ping1: TPing;
Label2: TLabel;
ComboBox_enable: TComboBox;
test_id: TEdit;
DataSource1: TDataSource;
Query1: TQuery;
ComboBox_hour: TComboBox;
Label3: TLabel;
ComboBox_min: TComboBox;
Label14: TLabel;
DBGridEh1: TDBGridEh;
DBGridEh2: TDBGridEh;
DBGridEh3: TDBGridEh;
ComboBox_alert: TComboBox;
Timer1: TTimer;
pagecontrol1: TPageControl;
procedure ComboBox7Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button4Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;
{
type
tmythread = class(Tthread)
private
ptime:string;
sleeptime:integer;
function PingHost(HostIP: string): Boolean;
procedure OutputRes;
protected
procedure execute;
override ;
procedure giveanswer;
public
// procedure PingHost;
end;
}
var
Form_addtest: TForm_addtest;
test_status:string;
test_id:string;
var
exc:boolean;
i:integer;
pingtime:string;
implementation
uses frm_add_advance, dbmodule, maifrm, testthread, main;
{$R *.dfm}
procedure TForm_addtest.ComboBox7Change(Sender: TObject);
begin
pagecontrol2.ActivePageIndex:=combobox7.ItemIndex;
end;

procedure TForm_addtest.Button2Click(Sender: TObject);
begin
fm_addtest_advance.Show;
end;

procedure TForm_addtest.Button3Click(Sender: TObject);
var
hh:integer;
mm:integer;
temp:integer;
temptime:Ttime;
begin

// showmessage(inttostr(strtoint(combobox2.Text)-strtoint(formatdatetime('hh',now))*3600000));
temp:=(strtoint(combobox2.Text)-strtoint(formatdatetime('hh',now)))*3600000+(strtoint(combobox3.Text)-1-strtoint(formatdatetime('mm',now)))*60000+(60-strtoint(formatdatetime('ss',now)))*1000;
temptime:=strtotime(combobox2.Text+':'+combobox3.Text+':00');
showmessage(inttostr(secondsbetween(temptime,now)));
mainfrm.Edit_ip.Text:=ping_addr.Text;
mainfrm.Edit_pingtime.Text:=inttostr(temp);
testthread.tmythread.Create(false);
// Ping1.DnsLookup(ping_addr.Text);
// ping1.Address:='178.100.101.226';
// Ping1.Ping;
datamodule1.Query1.Close;
datamodule1.Query1.SQL.Clear;
//插入test_id_tbl表
datamodule1.Query1.SQL.Add('insert into test_id_tbl values(seq_testid.nextval,:test_name,:test_type,:memo,:related_url,:regular_id,:alert_status,:alert_action,:log_mod,:common_log,:enabled,sysdate,sysdate,:test_depend,:test_depend_id)');
datamodule1.Query1.ParamByName('test_name').Value:=test_name.Text;
datamodule1.Query1.ParamByName('test_type').Value:=combobox7.Text;
datamodule1.Query1.ParamByName('memo').Value:=url_memo.Text;
datamodule1.Query1.ParamByName('related_url').Value:=url_related.Text;
datamodule1.Query1.ParamByName('regular_id').Value:=pagecontrol1.ActivePage.Caption;
datamodule1.Query1.ParamByName('alert_status').Value:=combobox_alert.ItemIndex;
datamodule1.Query1.ParamByName('alert_action').Value:=combobox5.Text;
datamodule1.Query1.ParamByName('log_mod').Value:=combobox7.Text;
datamodule1.Query1.ParamByName('common_log').Value:='';
datamodule1.Query1.ParamByName('enabled').Value:=combobox_enable.Text;
// datamodule1.Query1.ParamByName('create_time').AsDate:=to_date('''+FromatdateTime('YYYY-MM-DD',NOW)+''',''yyyy-mm-dd'');
// datamodule1.Query1.ParamByName('modify_time').AsDate:=to_date('''+FromatdateTime('YYYY-MM-DD',NOW)+''',''yyyy-mm-dd'');
//是否有测试依赖
if combobox_depend.
Text <> '' then
begin
datamodule1.Query1.ParamByName('test_depend_id').Value:=combobox_depend.
Text;
datamodule1.Query1.ParamByName('test_depend').Value:='true';
end
else
begin
datamodule1.Query1.ParamByName('test_depend').Value:='false';
datamodule1.Query1.ParamByName('test_depend_id').value:='';
end;

datamodule1.Query1.ExecSQL;
datamodule1.Query1.Close;
//获得test_id
{
datamodule1.Query2.Close;
datamodule1.Query2.Database.Connected:=false;
datamodule1.Query2.Database.Connected:=True;
datamodule1.Query2.SQL.Clear;
datamodule1.Query2.SQL.Add('select max(test_id) from test_id_tbl');
datamodule1.Query2.Open;
if datamodule1.Query2.RecordCount>0 then
begin
test_id:=datamodule1.Query2.DataSource.DataSet.Fields.Fields[0].AsString;
end;
}
{ //插入结果表test_result_tbl
datamodule1.Query1.Close;
datamodule1.Query1.SQL.Clear;
datamodule1.Query1.SQL.Add('insert into test_result_tbl values(seq_testid.CURRVAL,:test_status,:test_reply,:test_method,:alive,:dead,:unknown,:total_time,:alive_time,:dead_time,:tests,:passed_tests,:dead_tests,:average_time,:min_time,:max_time,sysdate,sysdate,sysdate)');
//datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_status').Value:=test_status;
datamodule1.Query1.ParamByName('test_reply').Value:=ping1.Reply.RTT;
datamodule1.Query1.ParamByName('test_method').Value:=Combobox7.Text;
datamodule1.Query1.ParamByName('alive').Value:=100;
datamodule1.Query1.ParamByName('dead').Value:=0;
datamodule1.Query1.ParamByName('unknown').Value:=0;
datamodule1.Query1.ParamByName('total_time').Value:=1;
datamodule1.Query1.ParamByName('alive_time').Value:=1;
datamodule1.Query1.ParamByName('dead_time').Value:=0;
datamodule1.Query1.ParamByName('tests').Value:=1;
datamodule1.Query1.ParamByName('passed_tests').Value:=1;
datamodule1.Query1.ParamByName('dead_tests').Value:=1;
datamodule1.Query1.ParamByName('average_time').Value:=0;
datamodule1.Query1.ParamByName('min_time').Value:=1;
datamodule1.Query1.ParamByName('max_time').Value:=1;
// datamodule1.Query1.ParamByName('change_time').Value:='';
// datamodule1.Query1.ParamByName('start_time').Value:='';
// datamodule1.Query1.ParamByName('end_time').Value:='';
datamodule1.Query1.ExecSQL;
}
//插入ping_properties_tbl表
if ( combobox7.ItemIndex = 0 ) then
begin
datamodule1.Query1.Close;
datamodule1.query1.sql.clear;
datamodule1.Query1.SQL.Add('insert into ping_properties_tbl values(seq_testid.CURRVAL,:test_url,:timeout,:packet_size,:packets,:deadline)');
// datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_url').Value:=ping_addr.Text;
datamodule1.Query1.ParamByName('timeout').Value:=ping_timeout.Text;
datamodule1.Query1.ParamByName('packet_size').Value:=ping_size.Text;
datamodule1.Query1.ParamByName('packets').Value:=ping_nums.Text;
datamodule1.Query1.ParamByName('deadline').Value:=ping_deadline.Text;
datamodule1.Query1.ExecSQL;
// datamodule1.Query1.Close;
end
else
//否则插入url_properties_tbl表
begin
datamodule1.Query1.Close;
datamodule1.query1.sql.clear;
datamodule1.Query1.SQL.Add('insert into url_properties_tbl values(seq_testid.CURRVAL,:test_url,:timeout,:advanced_set)');
//datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_url').Value:=url_address.Text;
datamodule1.Query1.ParamByName('timeout').Value:=url_timeout.Text;
datamodule1.Query1.ParamByName('advanced_set').Value:='false';
datamodule1.Query1.ExecSQL;
// datamodule1.Query1.Close;
end;
//是常规日程
if pagecontrol1.ActivePageIndex=0 then
begin
datamodule1.Query1.Close;
datamodule1.query1.sql.clear;
datamodule1.Query1.SQL.Add('insert into regular_tbl values(seq_testid.CURRVAL,:regular_schedule,:hour,:minute)');
datamodule1.Query1.ParamByName('regular_schedule').Value:=combobox4.Text;
datamodule1.Query1.ParamByName('hour').Value:=combobox2.Text;
datamodule1.Query1.ParamByName('minute').Value:=combobox3.Text;
datamodule1.Query1.ExecSQL;
end
else
//非常规日程
begin
datamodule1.Query1.Close;
datamodule1.query1.sql.clear;
datamodule1.Query1.SQL.Add('insert into irregular_tbl values(seq_testid.CURRVAL,:year,:month,:day,:hour,:minute)');
datamodule1.Query1.ParamByName('year').Value:=combobox_year.Text;
datamodule1.Query1.ParamByName('month').Value:=combobox_month.Text;
datamodule1.Query1.ParamByName('day').Value:=combobox_day.Text;
datamodule1.Query1.ParamByName('hour').Value:=combobox_hour.Text;
datamodule1.Query1.ParamByName('minute').Value:=combobox_min.Text;
datamodule1.Query1.ExecSQL;
end;
form_addtest.Close;
mainfrm.DBGridEh1.DataSource.DataSet.Active:=false;
mainfrm.DBGridEh1.DataSource.DataSet.Active:=true;
//threed part
// exc:=true;
{
if pagecontrol1.ActivePage.Name='tabsheet1' then
begin
hh:=strtoint(combobox2.Text);
mm:=strtoint(combobox3.Text);
showmessage(inttostr((strtoint(formatdatetime('hh',now))-strtoint(combobox2.Text))*3600000+(strtoint(formatdatetime('mm',now))-strtoint(combobox3.Text))*60000+strtoint(formatdatetime('mm',now))*1000));
//showmessage(formatdatetime('hh:mm:ss',now)-formatdatetime('hh:mm:ss',now));
end;
}
// if combobox_enable.Text='Enabled' then
// tmythread.Create(false);
end;

{
procedure TForm_addtest.Ping1DnsLookupDone(Sender: TObject;
Error: Word);
begin
Ping1.Address := Ping1.DnsResult;
Ping1.Ping;
end;

procedure TForm_addtest.Ping1EchoReply(Sender, Icmp: TObject;
Status: Integer);
begin

if Status <> 0 then
begin
//url_memo.Text:='Received ' + IntToStr(Ping1.Reply.DataSize) +' bytes from ' + Ping1.HostIP +' in ' + IntToStr(Ping1.Reply.RTT) + ' msecs';
test_status:='ok';
url_memo.Text:='ok';
end
else

test_status:='bad';
// url_memo.Text:='Cannot ping host (' + Ping1.HostIP + ') : ' +Ping1.ErrorString +'. Status = ' + IntToStr(Ping1.Reply.Status);
url_memo.Text:='bad';
end;
}
procedure TForm_addtest.Button5Click(Sender: TObject);
begin
form_addtest.Close;
end;

procedure TForm_addtest.FormShow(Sender: TObject);
begin
{ Query1.Close;
query1.sql.clear;
Query1.SQL.Add('select * from test_id_tbl where test_id=:test_id');
Query1.ParamByName('test_id').Value:=test_id.Text;
Query1.Open;
// if not Query1.DataSource.DataSet.EOF then
// begin
// query1.DataSource.DataSet.First;
test_name.Text:=Query1.DataSource.DataSet.Fields.Fields[1].Value;
// end;
}
end;

procedure TForm_addtest.Timer1Timer(Sender: TObject);
begin
//在定时中触发线程ping执行
if formatdatetime('hh:mm:ss',now)='14:03:30' then
begin
exc:=true;
// tmythread.Create(false);
end;
end;

procedure TForm_addtest.Button4Click(Sender: TObject);
var dd,ddd:Ttime;
temp:string;
begin

ddd:=strtotime('02:02:02');
dd:=strtotime(combobox2.Text+':'+combobox3.Text+':00');
//showmessage(combobox2.Text+':'+combobox3.Text+':00');
showmessage(formatdatetime('hh:mm:ss',now));
// showmessage(timetostr(formatdatetime('hh:mm:ss',now)-dd));
// showmessage(inttostr(strtoint(formatdatetime('hh',now))-hh)*60*60+strtoint(formatdatetime('mm',now))-mm)*60));
showmessage(inttostr(secondsbetween(now,dd)));
// testthread.tmythread.Create(false);
{ tmythread.Create(false);
}
end;

end.

-----------
线程程序 testthread
unit testthread;
interface
uses
Messages,Dialogs, SysUtils, Variants,WinSock,Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};
{
type
test = class(TThread)
private
procedure SetName;
protected
procedure Execute;
override;
end;
}
type
tmythread = class(Tthread)
private
ptime:string;
sleeptime:integer;
function PingHost(HostIP: string): Boolean;
//function gettime(time:integer):Integer;
//procedure OutputRes;
protected
procedure execute;
override ;
procedure giveanswer;
public
// procedure PingHost;
end;
implementation
uses test2, dbmodule, addtest, maifrm;
{ 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 test.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{$IFDEF MSWINDOWS}
type
TThreadNameInfo = record
FType: LongWord;
// must be 0x1000
FName: PChar;
// pointer to name (in user address space)
FThreadID: LongWord;
// thread ID (-1 indicates caller thread)
FFlags: LongWord;
// reserved for future use, must be zero
end;
{$ENDIF}
{ test }
procedure tmythread.execute;
var temp:integer;
begin
freeonterminate:=true;
temp:=strtoint(mainfrm.Edit_pingtime.Text);
sleep(temp);
synchronize(giveanswer);

// form_addtest.Ping1.DnsLookup(form_addtest.ping_addr.Text);
// form_addtest.Ping1.Address:=form_addtest.Ping1.DnsResult;
// form_addtest.Ping1.Ping;
// showmessage(IntToStr(form_addtest.Ping1.Reply.Status));
// form_addtest.Ping1.Address:=form_addtest.Ping1.DnsResult;
// form_addtest.Ping1.Ping;
// form_addtest.url_related.Text :=form_addtest.url_related.Text+IntToStr(form_addtest.Ping1.Reply.Status);
// if formatdatetime('hh:mm:ss',now)='15:36:00' then
// begin
{
if PingHost(form_addtest.ping_addr.Text) then
form_addtest.url_related.Text:='pingtime:'+pingtime+'ms';
else
form_addtest.url_related.Text :='ping is bad';
}
end;

procedure tmythread.giveanswer;
begin
// form_addtest.Ping1.DnsLookup(form_addtest.ping_addr.Text);
// form_addtest.Ping1.Address:=form_addtest.ping_addr.Text;
// form_addtest.Ping1.Ping;
// form_addtest.url_related.Text:=form_addtest.url_related.Text+IntToStr(form_addtest.Ping1.Reply.Status);
if PingHost(form_addtest.ping_addr.Text) then
begin
form_addtest.url_related.Text:='pingtime:'+ptime+'ms';
showmessage(ptime);
datamodule1.Query1.Close;
datamodule1.Query1.SQL.Clear;
datamodule1.Query1.SQL.Add('insert into test_result_tbl values(seq_testid.nextval,:test_status,:test_reply,:test_method,:alive,:dead,:unknown,:total_time,:alive_time,:dead_time,:tests,:passed_tests,:dead_tests,:average_time,:min_time,:max_time,sysdate,sysdate,sysdate)');
//datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_status').Value:='ok';
datamodule1.Query1.ParamByName('test_reply').Value:=ptime;
datamodule1.Query1.ParamByName('test_method').Value:=form_addtest.Combobox7.Text;
datamodule1.Query1.ParamByName('alive').Value:=100;
datamodule1.Query1.ParamByName('dead').Value:=0;
datamodule1.Query1.ParamByName('unknown').Value:=0;
datamodule1.Query1.ParamByName('total_time').Value:=1;
datamodule1.Query1.ParamByName('alive_time').Value:=1;
datamodule1.Query1.ParamByName('dead_time').Value:=0;
datamodule1.Query1.ParamByName('tests').Value:=1;
datamodule1.Query1.ParamByName('passed_tests').Value:=1;
datamodule1.Query1.ParamByName('dead_tests').Value:=1;
datamodule1.Query1.ParamByName('average_time').Value:=0;
datamodule1.Query1.ParamByName('min_time').Value:=1;
datamodule1.Query1.ParamByName('max_time').Value:=1;
// datamodule1.Query1.ParamByName('change_time').Value:='';
// datamodule1.Query1.ParamByName('start_time').Value:='';
// datamodule1.Query1.ParamByName('end_time').Value:='';
datamodule1.Query1.ExecSQL;
datamodule1.Query1.Close;
end
else
begin
form_addtest.url_related.Text :='ping is bad';
datamodule1.Query1.Close;
datamodule1.Query1.SQL.Clear;
datamodule1.Query1.SQL.Add('insert into test_result_tbl values(seq_testid.nextval,:test_status,:test_reply,:test_method,:alive,:dead,:unknown,:total_time,:alive_time,:dead_time,:tests,:passed_tests,:dead_tests,:average_time,:min_time,:max_time,sysdate,sysdate,sysdate)');
//datamodule1.Query1.ParamByName('test_id').Value:=test_id;
datamodule1.Query1.ParamByName('test_status').Value:='bad';
datamodule1.Query1.ParamByName('test_reply').Value:='unavailable';
datamodule1.Query1.ParamByName('test_method').Value:=form_addtest.Combobox7.Text;
datamodule1.Query1.ParamByName('alive').Value:=100;
datamodule1.Query1.ParamByName('dead').Value:=0;
datamodule1.Query1.ParamByName('unknown').Value:=0;
datamodule1.Query1.ParamByName('total_time').Value:=1;
datamodule1.Query1.ParamByName('alive_time').Value:=1;
datamodule1.Query1.ParamByName('dead_time').Value:=0;
datamodule1.Query1.ParamByName('tests').Value:=1;
datamodule1.Query1.ParamByName('passed_tests').Value:=1;
datamodule1.Query1.ParamByName('dead_tests').Value:=1;
datamodule1.Query1.ParamByName('average_time').Value:=0;
datamodule1.Query1.ParamByName('min_time').Value:=1;
datamodule1.Query1.ParamByName('max_time').Value:=1;
// datamodule1.Query1.ParamByName('change_time').Value:='';
// datamodule1.Query1.ParamByName('start_time').Value:='';
// datamodule1.Query1.ParamByName('end_time').Value:='';
datamodule1.Query1.ExecSQL;
datamodule1.Query1.Close;
end;
end;
{
function tmythread.gettime(time:integer):Integer;
begin
Result:=time;
end;
}
function tmythread.PingHost(HostIP:string): Boolean;
type
PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation = packed record
TTL: Byte;
TOS: Byte;
Flags: Byte;
OptionsSize: Byte;
OptionsData: PChar;
end;

PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply = packed record
Address: DWORD;
Status: DWORD;
RTT: DWORD;
DataSize: Word;
Reserved: Word;
Data: Pointer;
Options: TIPOptionInformation;
end;
TIcmpCreateFile = function: THandle;
stdcall;
TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean;
stdcall;
TIcmpSendEcho = function(IcmpHandle:THandle;
DestinationAddress: DWORD;
RequestData: Pointer;
RequestSize: Word;
RequestOptions: PIPOptionInformation;
ReplyBuffer: Pointer;
ReplySize: DWord;
Timeout: DWord
): DWord;
stdcall;
var
hICMP : THandle;
hICMPdll : THandle;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle : TIcmpCloseHandle;
IcmpSendEcho : TIcmpSendEcho;
pIPE : PIcmpEchoReply;// ICMP Echo reply buffer
FIPAddress : DWORD;
FSize : DWORD;
FTimeOut : DWORD;
BufferSize : DWORD;
pReqData,pRevData:pChar;
MyString:string;
begin
Result := False;
hICMPdll := LoadLibrary('icmp.dll');
if hICMPdll = 0 then
exit;
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll,'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
if (hICMP = INVALID_HANDLE_VALUE) then
exit;
FIPAddress := inet_addr(PChar(HostIP));
MyString := 'Hello,World';
//send data buffer
pReqData := PChar(MyString);
FSize := 40;
//receive data buffer
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
GetMem(pRevData,FSize);
pIPE^.Data := pRevData;
FTimeOut := 4000;
try
Result := IcmpSendEcho(hICMP, FIPAddress, pReqData,
Length(MyString),nil,pIPE,BufferSize,FTimeOut)>0;
ptime:=IntToStr(pIPE^.RTT);

finally
IcmpCloseHandle(hICMP);
FreeLibrary(hICMPdll);
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;

end.
-------------
我这里创建一个测试后,创建一个线程,但并没有马上执行,而是通过sleep()来控制执行测试的时间,这个睡眠时间是通过addtest里面的temp传给mainfrm的一个edit_pingtime得到的,我增加了2个测试,分别在5分钟,和6分钟后执行测试,结果只有后面一个测试执行了。请教我的问题在哪里?应该如何改进??谢谢
 
testthread.tmythread.Create(false);
定义两个线程变量,或者使用一个线程数据来保存你定义的 线程变量/对象,而不是一个
 
本人还是不是很明白。两个线程变量定义在哪里?
 
我只是大致看了下,不知楼主的错误是否确实在此
(楼主贴出的代码实在是长:)
...
var
Form_addtest: TForm_addtest;
test_status:string;
test_id:string;
thread1,thread2:tmythread;//这里,定义成全局变量
...
 
谢谢回复。还有问题是:如果我定义了两个线程变量,thread1,thread2,但是我创建一个测试就会创建一个线程,那我不是需要创建无数个线程变量了啊
 
呵呵,你可以创建一个动态数组么,用它来维护你的一系列线程
 
哦。还有我如何把addtest这个form中的时间变量传给testthread中的一个变量?这个时间变量就是sleep的时间,每个测试的时间会不同的。是不是需要用到什么临界区之类的?
 
这个并没有涉及到并发的写操作,所以不需要临界区保护
thread1 :=TmyThread.Create(true);
thread1.SleepTime :=strToInt(self.Edit_pingtime.Text);
thread1.resume;
 
还有我这个测试的有常规性的,比如每天的12点0分执行。这样的话,我应该如何处理程序?我是不是要做个循环呢,执行后挂起,然后到一定时间有开启?是不是这样?有另外更好的处理方式吗?
 
这个么,另外写一个管理线程,或作一个服务程序,将其功能限制在这样的周期计划管理上,时间误差不妨设大一点儿,比如1~5分钟(保证自己使用运行,但又不能抢占CPU时间)。通讯可以采用发消息的模式
 
顶部