📄 thread.pas
字号:
unit Thread;
interface
uses
Classes, SysUtils, windows, Driver;
type
CheckThread = class(TThread)
private
{ Private declarations }
dwDuration : Longint;
bCyclic : Boolean;
ptAoCheck : PT_FAOCheck;
iWhichBuf : Smallint;
iStopped : Smallint;
lCurrentCount : Longint;
lIntCounter : Longint ;
lBfChgCounter : Longint ;
lOverrunCounter : Longint ;
iOverrun : Smallint;
iHalfready : Smallint;
protected
procedure Execute; override;
procedure adInterruptEvent;
procedure adBufChangeEvent;
procedure adOverrunEvent;
procedure adWatchdogEvent;
procedure adTerminateEvent;
public
constructor Create(dwDur : Longint; bCyc : Boolean);
end;
implementation
uses CheckForm, MainForm, EVENT;
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure CheckThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{*************************************************************
* Function: Handle the error code. If the input error code > 0,
* it means some error apperent. This function can
* show the error message to a message box and stop
* this application.
* Input: The error code.
* return: none
************************************************************* }
Function DoesErr(var lErrCode: LongInt): integer;
var
szErrMsg : string[100];
pszErrMsg : PChar;
begin
{Check the pressed error code}
If (lErrCode <> 0) Then
begin
pszErrMsg := @szErrMsg;
DRV_GetErrorMessage(lErrCode, pszErrMsg);
MessageBox(0, pszErrMsg, 'Thread Message', MB_OK);
DoesErr := 1;
end
else
DoesErr := 0;
end;
{ CheckThread }
constructor CheckThread.Create(dwDur : Longint; bCyc : Boolean);
begin
inherited Create(False);
dwDuration := dwDur;
bCyclic := bCyc;
with ptAoCheck do
begin
ActiveBuf := @iWhichBuf;
stopped := @iStopped;
CurrentCount := @lCurrentCount;
overrun := @iOverrun;
HalfReady := @iHalfready;
end;
FreeOnTerminate := True;
end;
procedure CheckThread.Execute;
var
lTickCount : Longint;
ptChkEvent : PT_CheckEvent;
wEventType : Word;
begin
{ Place thread code here }
lIntCounter := 0;
lBfChgCounter := 0;
lOverrunCounter := 0;
{Prepare structure for checking conversion status}
with ptChkEvent do
begin
EventType := @wEventType;
Milliseconds := dwDuration;
end;
{Calculation the terminate time}
lTickCount := GetTickCount + dwDuration + 60000;
{Cyclic get deiver's event}
while (Terminated = false) do
begin
{1. Get the Device event}
if DRV_CheckEvent(frmMain.lDriverHandle, ptChkEvent) <> 0 then
begin
{If cannot get event close and terminate thread}
MessageBox(0,'Check Event Error !','Thread Message',MB_OK);
exit;
end;
{2. Does it contain the Interrupt event?}
if (wEventType = ADS_EVT_AO_INTERRUPT) then
{process interrupt event}
adInterruptEvent();
{3. Does it contain the buffer change event?}
if (wEventType = ADS_EVT_AO_BUFCHANGE) then
{process buffer change event}
adBufChangeEvent();
{4. Does it contain Overrun event?}
if (wEventType = ADS_EVT_AO_UNDERRUN) then
{process overrun event}
adOverrunEvent();
{5. Does it contain terminate event?}
if (wEventType = ADS_EVT_TERMINATED) then
begin
{process terminate event}
adTerminateEvent();
exit;
end;
frmCheck.labBuffChange.Caption := IntToStr(lBfChgCounter);
frmCheck.labInterrupt.Caption := IntToStr(lIntCounter);
frmCheck.labOverrun.Caption := IntToStr(lOverrunCounter);
end;
end;
procedure CheckThread.adInterruptEvent();
begin
{Process Interrupt event}
lIntCounter := lIntCounter+1;
end;
procedure CheckThread.adBufChangeEvent();
var
lErrCde : Longint;
begin
{Process Buffer change event}
lBfChgCounter := lBfChgCounter+1;
lErrCde := DRV_FAOCheck(frmMain.lDriverHandle, ptAoCheck);
if DoesErr(lErrCde) = 1 then
exit;
end;
procedure CheckThread.adOverrunEvent();
var
lErrCde : Longint;
begin
{Process overrun event.
This demo does not care the overrun event, just clear it}
lOverrunCounter := lOverrunCounter+1;
frmCheck.labOverrun.Caption := IntToStr(lOverrunCounter);
lErrCde := DRV_ClearOverrun(frmMain.lDriverHandle);
if DoesErr(lErrCde) = 1 then
exit;
end;
procedure CheckThread.adWatchdogEvent();
begin
{Processing Watchdog event.}
// frmConverting.memEvent.Lines.Append('WatchDog Event');
end;
procedure CheckThread.adTerminateEvent();
begin
{Close the converting form and make it to retrieved convered data}
// frmCheck.Close;
frmMain.StopCheck();
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -