请教一个简单而有奇怪的问题(200分)

  • 主题发起人 主题发起人 leiing
  • 开始时间 开始时间
L

leiing

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个过程:
procedure one;
begin
语句1;//是一个非常耗时的语句
语句2;
...
end;

因为语句1是个发送的语句,要几秒才能返回值,很慢。我想判断语句1是否在2秒钟内执行完成,完成后继续执行语句2,如没有完成,则终止她,执行语句2。
请问如何实现?
 
barton半夜都在论坛,
实在令人佩服!
 
只好用线程了
 
用时钟来进行计数,判断计数值
试试看
 
procedure one;
var
time1: cardinal;
w: boolean;
begin
time1 := GetTickCount;
w := true;
while (GetTickCount - time1 < 2000) and wdo
begin

语句1;//是一个非常耗时的语句
w := // 语句1正常完成时置为 flse;
Application.ProcessMessage;
end;
语句2;
...
end;
 
此 barton` 非彼 barton ,注意那个点
如此回答,怀疑是魔鬼大"尸"的马甲,像它一惯的作风....
此类问题我所知道的,只有用线程解决,不熟,不熟...
 
肯定要用到多线程处理,要不把ONE放到一个定时器里面处理也行,
设二个变量V1和V2:
procedure one;
begin
V1:=FALSE;
语句1;//是一个非常耗时的语句
V1:=TRUE;
语句2;
...
end;
语句1里面增加一些处理 WHILE V2do
语句1
在别的语句里面控制V2就行了,如要终止1,只要设置V2:=FALSE.
 
对头,,:)
搞非了。。不好意思哈,,
确实该用线程来解决。。
TSend = class(TThread)
private
FSend: boolean;
protected
procedure Execute;
override;
public
property Sended: boolean read FSend;
end;

把你的 "语句1" 写到 execute 里, 在发送完成的时候置一个标志位。(假如就是 Sended);
procedure one;
var
time1: cardinal;
tmpSend: TSend;
begin
time1 := GetTickCount;
w := true;
tmpSend := TSend.
Create(true);

while (GetTickCount - time1 < 2000) and (not tmpSend.
Sended)do
begin

Application.ProcessMessage;
end;

//
if not tmpSend.
Sended then
tmpSend.
Terminate;
语句2;
...
end;


 
可以直接检测 线程的 Terminated
 
要中止它,要使用多线程
 
if visible = false then
exit;
if width = 0 then
exit;
with Controldo
begin
LBorder:= Rect(0,0,self.width,ClientHeight);
RBorder:= Rect(ClientWidth-Self.Width,0,ClientWidth,ClientHeight);
TBorder:= Rect(0,0,ClientWidth,Self.Width);
BBorder:= Rect(0,ClientHeight-Self.Width,ClientWidth,ClientHeight);
end;
if GradientFill then
begin
DrawGradRect(Canvas,LBorder,GradientStartColor,GradientEndColor,jgtHorz);
DrawGradRect(Canvas,TBorder,GradientStartColor,GradientEndColor,jgtVert);
DrawGradRect(Canvas,RBorder,GradientStartColor,GradientEndColor,jgtHorz);
DrawGradRect(Canvas,BBorder,GradientStartColor,GradientEndColor,jgtVert);
end
else
begin
SavedColor:= Canvas.Brush.Color;
Canvas.brush.color:= Color;
DrawRect(Canvas,LBorder);
DrawRect(Canvas,TBorder);
DrawRect(Canvas,RBorder);
DrawRect(Canvas,BBorder);
Canvas.Brush.Color:= SavedColor;
end;

{draw the corners}
if CornerVisible then
begin
Canvas.Brush.Color:= SavedColor;
Canvas.brush.color:= CornerColor;
CornerLength:= 20;
RBorder.Right:= RBorder.Right-1;
BBorder.Bottom:= BBorder.Bottom-1;
with LBorderdo
begin
Canvas.Polygon([Point(0,0),Point(right,width),Point(width,CornerLength),Point(0,CornerLength)]);
Canvas.Polygon([Point(0,bottom),Point(0,bottom-CornerLength),Point(width,bottom-CornerLength),Point(width,Bottom-width)]);
end;

with TBorderdo
begin
Canvas.Polygon([Point(0,0),Point(CornerLength,0),Point(CornerLength,width),Point(width,width)]);
Canvas.Polygon([Point(right,0),Point(right-width,width),Point(right-CornerLength,width),Point(right-CornerLength,0)]);
end;

with RBorderdo
begin
Canvas.Polygon([Point(right,0),Point(right,CornerLength),Point(right-width,CornerLength),Point(right-width,width)]);
Canvas.Polygon([Point(right,bottom),Point(right-width,bottom-width),Point(right-width,bottom-CornerLength),Point(right,bottom-CornerLength)]);
end;

with BBorderdo
begin
Canvas.Polygon([Point(right,bottom),Point(right-CornerLength,bottom),Point(right-CornerLength,bottom-width),Point(right-width,bottom-width)]);
Canvas.Polygon([Point(0,bottom),Point(width,bottom-width),Point(CornerLength,bottom-width),Point(CornerLength,bottom)]);
end;
end;
 
可以创建一个互斥量,然后调用waitForSIngleObject.
 
后退
顶部