-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoCustomActorMatching.cs
More file actions
73 lines (59 loc) · 2.02 KB
/
Copy pathDemoCustomActorMatching.cs
File metadata and controls
73 lines (59 loc) · 2.02 KB
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
using System;
using System.Windows.Forms;
using SynchronousMessageSystem;
namespace DemoApplication;
public partial class DemoCustomActorMatching : Form
{
private ActorSystem ActorSystem { get; }
private CustomActorMatch? MatchIfValueIsOne { get; set; }
private CustomActorMatch? MatchIfValueIsTwo { get; set; }
public DemoCustomActorMatching()
{
InitializeComponent();
// 1. Create the actor system.
ActorSystem = new ActorSystem();
}
private void DemoCustomActorMatching_Load(object sender, EventArgs e)
{
// 2. Create the actors.
var hasOne = new Actor1E(1);
var hasTwo = new Actor1E(2);
// 3. Create match conditions.
MatchIfValueIsOne = new CustomActorMatch(nameof(Actor1E.MyReceiver), 1);
MatchIfValueIsTwo = new CustomActorMatch(nameof(Actor1E.MyReceiver), 2);
// 4. Add actors to actorsystem.
ActorSystem.AddActor(hasOne);
ActorSystem.AddActor(hasTwo);
}
private void button1_Click(object sender, EventArgs e)
{
// 5. Talk to all who matches the criteria.
ActorSystem.TalkToAll(MatchIfValueIsOne!, "One"); // Hits both receivers.
ActorSystem.TalkToAll(MatchIfValueIsTwo!, "Two"); // Hits only the second receiver.
}
}
public class CustomActorMatch : ActorMatch
{
public int SearchForValue { get; }
public CustomActorMatch(string receiveProcess, int searchForValue) : base(null, receiveProcess)
{
SearchForValue = searchForValue;
}
public override bool IsMatch(object message) =>
(message as Actor1E)?.Value >= SearchForValue;
}
public class Actor1E : Actor
{
public int Value { get; }
public Actor1E(int value)
{
Value = value;
}
public override void Other(Actor sender, ActorMatch? address, object message)
{
}
public void MyReceiver(Actor sender, ActorMatch address, object message)
{
MessageBox.Show($@"Found instance with value {Value}, got message: {message}");
}
}