﻿Option Strict Off
Option Explicit On
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1
    Dim TimerId As Short
    Dim IntervalCount As Integer
    Dim gCh As GCHandle
    Dim pdelegate_func As PTM_INTERVAL_CALLBACK
    Dim pfunc As IntPtr

    Private Sub IntervalCallback_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '--------------------------------------
        ' Initialize the delegate for Interval Timer event processing
        '--------------------------------------
        pdelegate_func = New PTM_INTERVAL_CALLBACK(AddressOf CallBackProc)
        '--------------------------------------
        ' Do not release the delegate
        '--------------------------------------
        If gCh.IsAllocated = False Then
            gCh = GCHandle.Alloc(pdelegate_func)
        End If
    End Sub

    '==============================================================================
    ' TimInit
    '==============================================================================
    Private Sub Button_TimInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimInit.Click
        Dim DeviceName As New String("", 256)
        Dim ProductName As New String("", 256)
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' Get Device Name
        '-----------------------------------
        DeviceName = Text_DeviceName.Text.PadRight(256, ControlChars.NullChar)
        '-----------------------------------
        ' Initialization
        '-----------------------------------
        Ret = TimInit(DeviceName, ProductName)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimInit ret = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            Text_DeviceName.Text = DeviceName
            Label_ProductName.Text = ProductName
        End If
    End Sub

    '==============================================================================
    ' TimStartTimer
    '==============================================================================
    Private Sub Button_TimStartTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimStartTimer.Click
        Dim Microseconds As Integer
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' Interval Count Initialization
        '-----------------------------------
        IntervalCount = 0
        '-----------------------------------
        ' Get the specified value of Interval
        '-----------------------------------
        Microseconds = Text_Interval.Text
        '-----------------------------------
        ' Start Interval Timer
        '-----------------------------------
        Ret = TimStartTimer(TimerId, Microseconds, Handle.ToInt32)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimStartTimer = " & Ret & " " & ErrorString.ToString()
        If Ret <> TIM_ERR_SUCCESS Then
            Exit Sub
        End If
        '-----------------------------------
        ' Get the fixed pointer of the delegate for for Interval Timer event processing
        '-----------------------------------
        pfunc = Marshal.GetFunctionPointerForDelegate(pdelegate_func)
        '-----------------------------------
        ' Interval Timer Callback registration
        '-----------------------------------
        Dim Temp As IntPtr = New IntPtr(TimerId)
        Ret = TimTimerCallBackProc(TimerId, pfunc, Temp)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimTimerCallBackProc = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            '-----------------------------------
            ' Enable the TimStopTimer Button
            '-----------------------------------
            Button_TimStopTimer.Enabled = True
            '-----------------------------------
            ' Disable the TimStartTimer Button
            '-----------------------------------
            Button_TimStartTimer.Enabled = False
        End If
    End Sub

    '==============================================================================
    ' TimStopTimer
    '==============================================================================
    Private Sub Button_TimStopTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimStopTimer.Click
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' Stop Interval Timer
        '-----------------------------------
        Ret = TimStopTimer(TimerId)
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimStopTimer = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            '-----------------------------------
            ' Disable the TimStopTimer button
            '-----------------------------------
            Button_TimStopTimer.Enabled = False
            '-----------------------------------
            ' Enable the TimStartTimer button
            '-----------------------------------
            Button_TimStartTimer.Enabled = True
        End If
    End Sub

    '==============================================================================
    ' TimExit
    '==============================================================================
    Private Sub Button_TimExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TimExit.Click
        Dim ErrorString As New StringBuilder("", 256)
        Dim Ret As Integer

        '-----------------------------------
        ' Exit handle
        '-----------------------------------
        Ret = TimExit()
        '-----------------------------------
        ' Error handle
        '-----------------------------------
        Call TimGetErrorString(Ret, ErrorString)
        Text_ReturnCode.Text = "TimExit = " & Ret & " " & ErrorString.ToString()
        If Ret = TIM_ERR_SUCCESS Then
            '-----------------------------------
            ' Disable the TimStopTimer button
            '-----------------------------------
            Button_TimStopTimer.Enabled = False
            '-----------------------------------
            ' Enable the TimStartTimer button
            '-----------------------------------
            Button_TimStartTimer.Enabled = True
        End If
    End Sub

    '==============================================================================
    ' Exit
    '==============================================================================
    Private Sub Button_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
        '-----------------------------------
        ' Exit handle
        '-----------------------------------
        Call TimExit()
        '--------------------------------------
        ' Release the garbage collection handle of the delegate for Interval Timer event processing
        '--------------------------------------
        If gCh.IsAllocated = True Then
            gCh.Free()
        End If
        '-----------------------------------
        ' Exit
        '-----------------------------------
        End
    End Sub

    Delegate Sub SetCountDelegate()
    '==============================================================================
    ' Synchronous function for control operation
    '==============================================================================
    Sub SetCountText()
        Label_Msg_Count.Text = IntervalCount
    End Sub

    '==============================================================================
    ' Callback routine - Interval Timer event processing
    '==============================================================================
    Protected Sub CallBackProc(ByVal Id As Short, ByVal wParam As Integer, ByVal lParam As Integer, ByVal Param As IntPtr)
        IntervalCount = IntervalCount + 1
        '--------------------------------------
        ' Show the count value
        '--------------------------------------
        Invoke(New SetCountDelegate(AddressOf SetCountText))
    End Sub
End Class
