📄 thread.bas
字号:
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 bTimeCritical As Boolean
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 GetCurrentProcess Lib "kernel32" () As Long
Declare Function TerminateProcess Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
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 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 GetTickCount Lib "kernel32" () As Long
Declare Function ResumeThread Lib "kernel32" (ByVal hThread 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
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
lngEventCount = 0
'ptFDITransfer.EventType = ADS_EVT_INTERRUPT
'ptFDITransfer.RetData = DRV_GetAddress(gReturnData)
ptCheckEvent.EventType = DRV_GetAddress(lngEventType)
ptCheckEvent.Milliseconds = 1000
lngStartTime = GetTickCount()
While gbStopThread = False
'gReturnData = 0
'Check interrupt event
If (DRV_CheckEvent(DeviceHandle, ptCheckEvent) = SUCCESS) Then
lngEventCount = lngEventCount + 1
Debug.Print "ChenkEvent OK!"
Else
mainfrm.txtEventCount.Text = "0"
Debug.Print "CheckEvent Failed!"
End If
'If (lngEventType = ADS_EVT_INTERRUPT) Then
' 'Read port data when interrupt happened
' Response = DRV_FDITransfer(DeviceHandle, ptFDITransfer)
'End If
lngCurrentTime = GetTickCount()
lngTotalTime = lngCurrentTime - lngStartTime
'Caculate Interrupt Event Count
If (lngTotalTime > 1000) Then
gEventCount = CSng((CLng(lngEventCount) / CSng(lngTotalTime) * 1000#))
mainfrm.txtEventCount.Text = gEventCount
lngStartTime = GetTickCount()
lngEventCount = 0
End If
Wend
mainfrm.txtEventCount.Text = ""
gbThreadTerminated = True
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -