thread.bas

来自「16 relay output channels and 16 isolated」· BAS 代码 · 共 93 行

BAS
93
字号
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

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 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 TerminateThread& Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode 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
    Dim lngError As Long
        
    ptCheckEvent.EventType = DRV_GetAddress(lngEventType)
    ptCheckEvent.Milliseconds = 1000
    lngStartTime = GetTickCount()
    
    While gbStopThread = False
        'Check interrupt event
    
        lngError = DRV_CheckEvent(DeviceHandle, ptCheckEvent)
        
        'must check the return code and if the event type is ADS_EVT_INTERRUPT
         If (lngError = SUCCESS) Then
            If (lngEventType = ADS_EVT_INTERRUPT_DI0) Then
                lngEventCount = lngEventCount + 1
                gCounter = gCounter + 1
            End If
            'Debug.Print "CheckEvent Failed!"
        End If
        
        lngCurrentTime = GetTickCount()
        lngTotalTime = lngCurrentTime - lngStartTime
        
        'Caculate Interrupt Event Count
        If (lngTotalTime > 1000) Then
            gEventCount = CSng((CLng(lngEventCount) / CSng(lngTotalTime) * 1000#))
            lngEventCount = 0
            lngStartTime = GetTickCount()
        End If
             
        If gCounter > 100000 Then
            gCounter = gCounter - 100000
        End If
    Wend
    
    frmMain.txtIntData.Text = ""
    frmMain.txtEventCount.Text = ""
    gCounter = 0
    gbThreadTerminated = True
    
End Sub








⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?