-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrivial Intra process Com using VB.vb
82 lines (55 loc) · 2.16 KB
/
Trivial Intra process Com using VB.vb
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
Imports System.Threading
Module TrivialIntraprocessComusingVB
Public nCounter As Long
Public nShareMemory As Long
' Signaled event: the wicket is initialy open/signaled
Public eClientWicket As New AutoResetEvent(True)
' Unsignaled event the wicket is initialy closed/unsignaled
Public eServerWicket As New AutoResetEvent(False)
Public lWeOff As Boolean
Sub Main()
' Timer launch
Dim tState = New Timer(AddressOf State, Nothing, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1))
' Client (writer / applicant) thread launch
Dim tClient As New Thread(AddressOf Client)
tClient.Start()
' Server (reader / counter clerk) thread launch
Dim tServer As New Thread(AddressOf Server)
tServer.Start()
' Wait for Enter
Console.ReadLine()
lWeOff = True
Console.WriteLine("Main exit")
End Sub
Sub Client()
Do
' The wicket is initially open (signaled) and will close automatically after passing through (AutoResetevent)
eClientWicket.WaitOne()
' New application data...
nShareMemory += 1
' Open the server wicket
eServerWicket.Set()
Loop Until lWeOff
Console.WriteLine("Client thread exit")
End Sub
Sub Server()
Do
' The wicket is initialy closed (unsignaled) and will automatically close after passing through (AutoReset event)
eServerWicket.WaitOne()
' Some important job...
If nShareMemory = 100000 Then
nShareMemory = 0
End If
' Service counter (possible concurrency with State)
Interlocked.Increment(nCounter)
' Open the client wicket
eClientWicket.Set()
Loop Until lWeOff
Console.WriteLine("Server thread exit")
End Sub
Sub State()
Console.WriteLine($"Call per s.: {nCounter}")
' (possible concurrency with Server)
Interlocked.CompareExchange(nCounter, 0, nCounter)
End Sub
End Module