summaryrefslogtreecommitdiff
path: root/vb/demo/Ice/async/WorkQueue.vb
blob: d42d099ebb1fa2763123d90ac5a0dec5e66bd7a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
' **********************************************************************
'
' Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
'
' This copy of Ice is licensed to you under the terms described in the
' ICE_LICENSE file included in this distribution.
'
' **********************************************************************

Imports Demo
Imports System
Imports System.Collections
Imports System.Threading

Public Class WorkQueue

    Private Class CallbackEntry
        Public cb As AMD_Hello_sayHello
        Public delay As Integer
    End Class

    Public Sub Join()
        _thread.Join()
    End Sub

    Public Sub Start()
        _thread = New Thread(New ThreadStart(AddressOf Me.Run))
        _thread.Start()
    End Sub

    Public Sub Run()
        SyncLock Me
            While Not _done
                If _callbacks.Count = 0 Then
                    Monitor.Wait(Me)
                End If

                If Not _callbacks.Count = 0 Then
                    Dim entry As CallbackEntry = _callbacks(0)
                    Monitor.Wait(Me, entry.delay)

                    If Not _done Then
                        _callbacks.RemoveAt(0)
                        Console.WriteLine("Belated Hello World!")
                        entry.cb.ice_response()
                    End If
                End If
            End While

            Dim e As CallbackEntry
            For Each e In _callbacks
                e.cb.ice_exception(New RequestCanceledException())
            Next
        End SyncLock
    End Sub

    Public Sub Add(ByVal cb As AMD_Hello_sayHello, ByVal delay As Integer)
        SyncLock Me
            If Not _done Then
                Dim entry As CallbackEntry = New CallbackEntry
                entry.cb = cb
                entry.delay = delay

                If _callbacks.Count = 0 Then
                    Monitor.Pulse(Me)
                End If
                _callbacks.Add(entry)
            Else
                cb.ice_exception(New RequestCanceledException())
            End If
        End SyncLock
    End Sub

    Public Sub destroy()
        SyncLock Me
            _done = True
            Monitor.Pulse(Me)
        End SyncLock
    End Sub
    
    Private _thread As Thread
    Private _done As Boolean 
    Private _callbacks As ArrayList = New ArrayList
End Class