这里为什么出错呀?(50分)

  • 主题发起人 coolingxyz
  • 开始时间
C

coolingxyz

Unregistered / Unconfirmed
GUEST, unregistred user!
unit mythtead;
interface
uses
Classes, NMUDP, IniFiles, SysUtils, Forms;
type
Tmythread = class(TThread)
private
{ Private declarations }
NMUDP1 : TNMUDP;
//定义一个TNMUDP 实例

Public
{ Public declarations }
// Constructor Create(AOwner : TComponent);
Override;
protected
procedure Execute;
override;
procedure GetIni;
end;

implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Tmythread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ Tmythread }
procedure Tmythread.Execute;
var
MyData : Array[0..7] of Char;
begin
{ Place thread code here }
FreeOnTerminate := False;
//线程做完一遍后不终止
GetIni;
//NMUDP 初始化
MyData := '55410000';
NMUDP1.SendBuffer(MyData, 6);
Sleep(60000);
end;

procedure Tmythread.GetIni;
var
LPort, RPort : Integer;
WgIni: TIniFile;
begin
WgIni := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Wg.ini');
LPort := WgIni.ReadInteger('UDPINI','LocalPort',1025);
RPort := WgIni.ReadInteger('UDPINI','RemotePort',1024);
NMUDP1.LocalPort := LPort;
//这里出错
NMUDP1.RemotePort := RPort;
//这里出错
NMUDP1.RemoteHost :='255.255.255.255';
//这里出错
WgIni.Free;
end;

end.

我希望主程序打开后,有一个线程能每隔一分钟发一个广播包,同时有若干个客户端响应,应该怎么来设计这个程序呀。不能干扰主程序的运行。
 
TNMUDP 实例创建没有?
 
NMUDP1 := TNMUDP.Create(nil);

现在我加了这么一句,可以了。
但新的问题又来了。
sleep(60000);
//出错了;
主线程里用 MyThread := Tmythread.Create(false);
程序能跑起来。但过了一会,跳出对话框说:“Project wangguan.exe raised exception class UDPSockError with message 'Host Lookup Canceled',Process stopped.Use Step or Run to continue”;
这是怎么了呀?
 
听说sleep();函数一般不要用,那我这个程序里应该怎么来实现我的功能呢?
 
这个也可以delay
procedure Delay(x,y:word);
var
timeout:TDateTime;
begin
timeout:=now+encodeTime(0,x div 60,x mod 60,y);
While now<timeoutdo
Application.ProcessMessages;
end;
Delay(60,0);
 
To bubble
我照你 的意思改了。但还是出现原来的错误呀。
:“Project wangguan.exe raised exception class UDPSockError with message 'Host Lookup Canceled',Process stopped.Use Step or Run to continue”;
 
多人接受答案了。
 
顶部