thread.bas
来自「16 relay output channels and 16 isolated」· BAS 代码 · 共 156 行
BAS
156 行
Attribute VB_Name = "Thread"
Option Explicit
' Windows (Constant, Data structure, API) declaration
Public Const CREATE_SUSPENDED = &H4
Public Const THREAD_PRIORITY_NORMAL = &H0
Public Const THREAD_PRIORITY_TIME_CRITICAL = &H15
Public Const STILL_ACTIVE = &H103
Private Const MAXLONG = &H7FFFFFFF
Global ThreadHandle As Long
Global gbThreadTerminated As Boolean
Global gbStopThread As Boolean
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function GetTickCount Lib "kernel32" () As Long
Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Sub EventThread()
'NOTES:
'1. Do not try to set breakpoint in EvenThread(), this will crash the IDE.
'2. Use "Debug.Print" or something else to trace the thread
'3. Use Visual Bacsic 5.0 to compile this sample
'4. Because multi-thread programming was not be well-supported in Visual Basic,
' debug this sample may often crash the IDE. So you'd better compile this sample
' to a executable file and run it if you want to see the result.
Dim i As Long
ptCheckEvent.EventType = DRV_GetAddress(usEventType)
If (OverrunTime = 0) Then
ptCheckEvent.Milliseconds = MAXLONG
Else
ptCheckEvent.Milliseconds = OverrunTime
End If
While gbStopThread = False
'Check interrupt event
' Step1: check event type
ErrCde = DRV_CheckEvent(DriverHandle, ptCheckEvent)
Debug.Print "Check Event\n"
If ErrCde <> 0 Then
frmMain.lbReadyBuf.Caption = "Check Event Error!"
Debug.Print "Check Event Error\n"
End If
' Step2: take actions according each kind of event type
If usEventType And ADS_EVT_BUFCHANGE Then
BufchangeCounter = BufchangeCounter + 1
frmMain.lbBufChangeCnt.Caption = BufchangeCounter
Debug.Print "Buff change\n"
ErrCde = DRV_FAICheck(DriverHandle, ptFAICheck)
If HalfReady = 0 Then
frmMain.lbReadyBuf.Caption = "Not ready."
ElseIf HalfReady = 1 Then
frmMain.lbReadyBuf.Caption = "1st half ready."
ptFAITransfer.start = 0
ErrCde = DRV_FAITransfer(DriverHandle, ptFAITransfer)
Debug.Print "Buff1 change\n"
Else
frmMain.lbReadyBuf.Caption = "2nd half ready."
End If
End If
If usEventType And ADS_EVT_OVERRUN Then
OverrunCounter = OverrunCounter + 1
frmMain.lbOverrunCnt.Caption = OverrunCounter
End If
If usEventType And ADS_EVT_INTERRUPT Then
InterruptCounter = InterruptCounter + 1
frmMain.lbInterruptCnt.Caption = InterruptCounter
Debug.Print "InterruptCounter\n"
End If
If usEventType And ADS_EVT_TERMINATED Then
TerminateCounter = TerminateCounter + 1
Debug.Print "TerminateCounter\n"
gbStopThread = True
' Step 1: Stop A/D conversion for high speed
If CyclicMode1 <> MODE_Cyclic Then
ErrCde = DRV_FAITerminate(DriverHandle)
If ErrCde <> 0 Then
DRV_GetErrorMessage ErrCde, ErrMsg
MsgBox ErrMsg, vbOKOnly, "Driver Message"
DRV_FreeDMABuffer DriverHandle, pBuffer
DRV_DeviceClose DriverHandle
SetState (STATE_Ready)
Exit Sub
End If
End If
' Step 2: Get real voltage of Buffer from driver memory buffer
ptFAITransfer.ActiveBuf = 0 ' single buffer
ptFAITransfer.DataType = DataType
ptFAITransfer.start = 0
ptFAITransfer.Count = DispNum
ptFAITransfer.Overrun = DRV_GetAddress(Overrun)
ErrCde = DRV_FAITransfer(DriverHandle, ptFAITransfer)
If ErrCde <> 0 Then
DRV_GetErrorMessage ErrCde, ErrMsg
MsgBox ErrMsg, vbOKOnly, "Driver Message"
DRV_DeviceClose DriverHandle
SetState (STATE_Ready)
Exit Sub
End If
Sleep (200)
frmMain.Timer1.Enabled = False
' Step 4: Close driver
DRV_DeviceClose DriverHandle
SetState (STATE_Ready)
End If
Wend
gbThreadTerminated = True
End Sub
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?