thread.bas
来自「16 relay output channels and 16 isolated」· BAS 代码 · 共 107 行
BAS
107 行
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
Global DeviceHandle As Long
Global DeviceNum As Long
Global ptDevGetFeatures As PT_DeviceGetFeatures
Global lpDevFeatures As DEVFEATURES
Global ErrCde As Long
Global szErrMsg As String * 80
Global ptEnableEvent As PT_EnableEvent
Global ptDiPattern As PT_DIPattern 'Enable event
Global ptDiStatus As PT_DIStatus 'Status change
Global ptCheckEvent As PT_CheckEvent
Global lpDioReadPort As PT_DioReadPortByte
Global gbRun As Boolean
Global gbUseEvent As Boolean
Global gCounter As Long
Global ThreadHandle As Long
Global gbThreadTerminated As Boolean
Global gbStopThread As Boolean
Global glIntCount As Long
Global glSCCount As Long
Global glPMCount As Long
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 lngStartTime As Long
Dim lngCurrentTime As Long
Dim lngEventType As Long
Dim lngEventCount As Long
Dim lngTotalTime As Long
Dim Response As Long
ptCheckEvent.EventType = DRV_GetAddress(lngEventType)
ptCheckEvent.Milliseconds = 1000
lngStartTime = GetTickCount()
While gbStopThread = False
'Check interrupt event
If (DRV_CheckEvent(DeviceHandle, ptCheckEvent) = SUCCESS) Then
Debug.Print "ChenkEvent OK!"
If (lngEventType = ADS_EVT_INTERRUPT) Then
'Read port data when interrupt happened
glIntCount = glIntCount + 1
End If
If (lngEventType = ADS_EVT_STATUSCHANGE) Then
'Read port data when interrupt happened
glSCCount = glSCCount + 1
End If
If (lngEventType = ADS_EVT_PATTERNMATCH) Then
'Read port data when interrupt happened
glPMCount = glPMCount + 1
End If
Else
Debug.Print "CheckEvent Failed!"
End If
Wend
gbThreadTerminated = True
End Sub
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?