Public Sub Release()
SyncLock Me
cnt += 1
If cnt = 0 And f Then
Dim cb As New EventHandler(AddressOf FireEvent)
ctl.BeginInvoke(cb)
f = False
cnt = 0
End If
End SyncLock
End Sub
Public Sub Wait()
SyncLock Me
f = True
If cnt = 0 And f Then
Dim cb As New EventHandler(AddressOf FireEvent)
ctl.BeginInvoke(cb)
f = False
cnt = 0
End If
End SyncLock
End Sub
Public Sub FireEvent()
RaiseEvent OnReset(Me, New EventArgs())
End Sub
End Class
'以上是类的代码
'主线程代码
Dim cnt As New MyCounter(Me)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'并绑定MyCounter类的OnReset事件,以接收通知
AddHandler cnt.OnReset, AddressOf cnt_Reset
cnt.Wait() '此行代码,用来最一次触发大量事件用
End Sub
'此过程用于触发大量事件
Private Sub cnt_Reset(ByVal sender As Object, ByVal e As EventArgs)
For i As Integer = 0 To objcnt - 1
'每开启一个线程,调用一次cnt.Enter(),使计数器自减
cnt.Enter()
System.Threading.ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf OnEventTimeClockThread))
Next
cnt.Wait() '调用cnt.Wait(),cnt对象在响应完成后将会发出事件通知
End Sub
'各个对象响应事件的代码,在响应完成后,需要调用cnt.Release,使计数器自增
Public Sub OnEventTimeClockThread()
Threading.Thread.Sleep(2000)
cnt.Release()
End Sub