【发布时间】:2015-04-09 13:12:43
【问题描述】:
我对线程还是很陌生。我想创建一个程序,在主线程创建必要的表单时测试有效的互联网连接。代码 sn-p 在构造函数的末尾停止,并出现“无法在正在运行或挂起的线程上调用 Start”错误。并且由于某种原因,主窗体在此错误后关闭
constructor TPingThread.Create(IDThread: Integer);
begin
Self.FID:=IDThread;
Self.FreeOnTerminate:=true;
end;
destructor TPingThread.Destroy;
begin
EndThread(FID);
inherited;
end;
procedure TPingThread.Execute;
var
iTimeOuts, K: Byte;
sWebpage: String;
begin
inherited;
iTimeOuts:=0;
FIdPing:=TIdHTTP.Create(nil);
for k:=1 to 3 do
begin
Try
FIdPing.ConnectTimeout:=2000;
sWebpage:=FIdPing.Get('http://www.google.co.za')
Except
On Exception do inc(iTimeOuts);
End;
end;
if iTimeOuts=3 then MessageDlg('A working internetconnection is needed to reset your password',mtWarning,[mbOK],0);
if iTimeOuts=0 then FInternetConnection:=false
else FInternetConnection:=true;
FreeAndNil(FIdPing);
end;
【问题讨论】:
-
您忘记在构造函数中调用
inherited。 -
并且不要在Execute中调用继承。
-
产生不兼容类型错误
-
继承创建(false); // 运行线程
-
@Marnu123 类型不兼容,因为您的构造函数是重载。您必须调用基本构造函数 -
inherited Create(true)来创建暂停的线程
标签: multithreading delphi