F
flyupwards
Unregistered / Unconfirmed
GUEST, unregistred user!
本人开发串口通讯,希望不中断cpu的工作,而做出延时处理,故未选用sleep,
并采用了多线程,其中,延时时间为300ms,将延时操作转换为一个循环变量的最大值,以便对不同cpu周期的微机都能适应。
但存在下述问题:
一是,延时处理的类函数每次据300ms求得的变量数不定,有时6600,有时15000,形如此类
类源码:
unit gSThread;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
Type
SThread = class(TThread)
Public
ifstop:boolean;
MaxWaitTime:Longint;
procedure Execute; override;
private
{ Private declarations }
protected
End;
implementation
{ SampleThread }
procedure SThread.Execute;
var
FirstTickCount, Now: Longint;
MaxNum,j:Longint;
begin
ifstop:=false;
j:=0;
MaxNum:=90000000;
FirstTickCount := GetTickCount;
MaxWaitTime:=9000000;
repeat
Application.ProcessMessages;
{ allowing access to other controls, etc. }
Now := GetTickCount;
j:=j+1;
if (Now - FirstTickCount >= 300) or(j>=MaxNum) or (Now < FirstTickCount) then
ifstop:=true;
until (Now - FirstTickCount >= 300) or(j>=MaxNum) or (Now < FirstTickCount);
MaxWaitTime:= j;
end;
end.
二是,延时的函数未达到延时的求,在数据传输时,因为时间不配,通讯不通讯读写数据。
主程序对延时函数的调用如下:var
gSThread1:SThread;
i:integer;
begin
gSThread1 := SThread.Create(False);
i:=0;
repeat
gSThread1.ifstop:=false;
gSThread1.Execute;
i:=i+1;
until (gSThread1.ifstop) or (i>=MaxWaitTime);
gSThread1.FreeOnTerminate := True;
end;
并采用了多线程,其中,延时时间为300ms,将延时操作转换为一个循环变量的最大值,以便对不同cpu周期的微机都能适应。
但存在下述问题:
一是,延时处理的类函数每次据300ms求得的变量数不定,有时6600,有时15000,形如此类
类源码:
unit gSThread;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
Type
SThread = class(TThread)
Public
ifstop:boolean;
MaxWaitTime:Longint;
procedure Execute; override;
private
{ Private declarations }
protected
End;
implementation
{ SampleThread }
procedure SThread.Execute;
var
FirstTickCount, Now: Longint;
MaxNum,j:Longint;
begin
ifstop:=false;
j:=0;
MaxNum:=90000000;
FirstTickCount := GetTickCount;
MaxWaitTime:=9000000;
repeat
Application.ProcessMessages;
{ allowing access to other controls, etc. }
Now := GetTickCount;
j:=j+1;
if (Now - FirstTickCount >= 300) or(j>=MaxNum) or (Now < FirstTickCount) then
ifstop:=true;
until (Now - FirstTickCount >= 300) or(j>=MaxNum) or (Now < FirstTickCount);
MaxWaitTime:= j;
end;
end.
二是,延时的函数未达到延时的求,在数据传输时,因为时间不配,通讯不通讯读写数据。
主程序对延时函数的调用如下:var
gSThread1:SThread;
i:integer;
begin
gSThread1 := SThread.Create(False);
i:=0;
repeat
gSThread1.ifstop:=false;
gSThread1.Execute;
i:=i+1;
until (gSThread1.ifstop) or (i>=MaxWaitTime);
gSThread1.FreeOnTerminate := True;
end;