大家好:
我做了一个线程,想让主程序,和线程程序同时操作。
主单元:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
TconnectThread.Resume();end;procedure TForm1.Timer2Timer(Sender: TObject);
begin
TconnectThread := Tconnect.Create(SocketConnection1);
TconnectThread.Priority := tpidle;
Timer1.Enabled := true;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
end;
多线程单元:
implementationuses Unit1;
const
cstTimeOut=50;
procedure Tconnect.ConnectSockt;
begin
try
Socket.Connected := true;
if Socket.Connected then
begin
Form1.Shape1.Brush.Color := ClGreen;
Suspend();
end
else begin
Form1.Shape1.Brush.Color := clred;
Suspend();
end;
except
Form1.Shape1.Brush.Color := clred;
end;
Form1.Timer1.Enabled := False;
end;constructor Tconnect.Create(Sckt: TSocketConnection);
begin
Wait_Event:=CreateEvent(nil,true,false,nil);
Socket := Sckt;
inherited Create(true);
end;procedure Tconnect.Execute;
begin
if WaitForSingleObject(Wait_Event,cstTimeOut)=WAIT_TIMEOUT then
begin
Synchronize(ConnectSockt);
end
else//结束线程
begin
Exit;
end;
end;
这个可以实现,但每次到线程的代码时,主程序的其它操作就停止了,我想让他们同时进行,请问有什么办法,请各位指教。
我做了一个线程,想让主程序,和线程程序同时操作。
主单元:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
TconnectThread.Resume();end;procedure TForm1.Timer2Timer(Sender: TObject);
begin
TconnectThread := Tconnect.Create(SocketConnection1);
TconnectThread.Priority := tpidle;
Timer1.Enabled := true;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
end;
多线程单元:
implementationuses Unit1;
const
cstTimeOut=50;
procedure Tconnect.ConnectSockt;
begin
try
Socket.Connected := true;
if Socket.Connected then
begin
Form1.Shape1.Brush.Color := ClGreen;
Suspend();
end
else begin
Form1.Shape1.Brush.Color := clred;
Suspend();
end;
except
Form1.Shape1.Brush.Color := clred;
end;
Form1.Timer1.Enabled := False;
end;constructor Tconnect.Create(Sckt: TSocketConnection);
begin
Wait_Event:=CreateEvent(nil,true,false,nil);
Socket := Sckt;
inherited Create(true);
end;procedure Tconnect.Execute;
begin
if WaitForSingleObject(Wait_Event,cstTimeOut)=WAIT_TIMEOUT then
begin
Synchronize(ConnectSockt);
end
else//结束线程
begin
Exit;
end;
end;
这个可以实现,但每次到线程的代码时,主程序的其它操作就停止了,我想让他们同时进行,请问有什么办法,请各位指教。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货