⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventwaithandle.vb

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 VB
字号:
Imports System.Threading
Imports System.Runtime.InteropServices

Public Enum EventResetMode
    AutoReset = 0
    ManualReset = 1
End Enum

Public Class EventWaitHandle
    Inherits WaitHandle

    Public Sub New(ByVal initialState As Boolean, ByVal mode As EventResetMode, ByVal name As String)
        Me.Handle = NativeMethods.CreateEvent(IntPtr.Zero, (mode = EventResetMode.ManualReset), initialState, name)
    End Sub

    Public Overrides Sub Close()
        If (Me.Handle <> WaitHandle.InvalidHandle) Then
            NativeMethods.CloseHandle(Me.Handle)
            Me.Handle = WaitHandle.InvalidHandle
        End If
    End Sub

    Public Function Reset() As Boolean
        Return NativeMethods.EventModify(Me.Handle, NativeMethods.EVENT.RESET)
    End Function

    Public Function [Set]() As Boolean
        Return NativeMethods.EventModify(Me.Handle, NativeMethods.EVENT.SET)
    End Function

    Public Shared Function WaitAny(ByVal waitHandles As WaitHandle()) As Integer
        Return EventWaitHandle.WaitAny(waitHandles, -1, False)
    End Function

    Public Shared Function WaitAny(ByVal waitHandles As WaitHandle(), ByVal millisecondsTimeout As Integer, ByVal exitContext As Boolean) As Integer
        Dim [handles] As IntPtr() = New IntPtr(waitHandles.Length - 1) {}
        Dim i As Integer = 0
        Do While (i < [handles].Length)
            [handles](i) = waitHandles(i).Handle
            i += 1
        Loop
        Return NativeMethods.WaitForMultipleObjects([handles].Length, [handles], False, millisecondsTimeout)
    End Function

    Public Overrides Function WaitOne() As Boolean
        Return Me.WaitOne(-1, False)
    End Function

    Public Overrides Function WaitOne(ByVal millisecondsTimeout As Integer, ByVal exitContext As Boolean) As Boolean
        Return (NativeMethods.WaitForSingleObject(Me.Handle, millisecondsTimeout) = 0)
    End Function

    Public Const WaitTimeout As Integer = &H102


    Friend Class NativeMethods

        <DllImport("coredll", SetLastError:=True)> _
        Friend Shared Function CloseHandle(ByVal hObject As IntPtr) As Boolean
        End Function

        <DllImport("coredll", SetLastError:=True)> _
        Friend Shared Function CreateEvent(ByVal lpEventAttributes As IntPtr, ByVal bManualReset As Boolean, ByVal bInitialState As Boolean, ByVal lpName As String) As IntPtr
        End Function

        <DllImport("coredll.dll", SetLastError:=True)> _
        Friend Shared Function EventModify(ByVal hEvent As IntPtr, ByVal ef As [EVENT]) As Boolean
        End Function

        <DllImport("coredll", SetLastError:=True)> _
        Friend Shared Function WaitForMultipleObjects(ByVal nCount As Integer, ByVal lpHandles As IntPtr(), ByVal fWaitAll As Boolean, ByVal dwMilliseconds As Integer) As Integer
        End Function

        <DllImport("coredll", SetLastError:=True)> _
        Friend Shared Function WaitForSingleObject(ByVal hHandle As IntPtr, ByVal dwMilliseconds As Integer) As Integer
        End Function

        Friend Enum [EVENT]
            PULSE = 1
            Reset = 2
            [Set] = 3
        End Enum

    End Class
End Class


⌨️ 快捷键说明

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