Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor to windowing

  • Loading branch information...
commit 4c8078f636c14f57e11df0e249d2ecd43993e6c3 1 parent 7667b63
@MikeBild authored
Showing with 79 additions and 80 deletions.
  1. +79 −80 RxTimeout/Form1.cs
View
159 RxTimeout/Form1.cs
@@ -9,101 +9,100 @@
namespace RxTimeout
{
- public partial class Form1 : Form
- {
- int _offset;
-
- public Form1()
+ public partial class Form1 : Form
{
- InitializeComponent();
-
- RxMessageBrokerMinimod.Default.Register<Message>(Message, new ControlScheduler(this));
- RxMessageBrokerMinimod.Default.Register<Start>(Start, TaskPoolScheduler.Default);
- }
+ int _offset;
- void Start(Start obj)
- {
- RxMessageBrokerMinimod.Default.Send(new Message("1"));
- // put Thread.Sleep(20) here and it works...
- RxMessageBrokerMinimod.Default.Send(new Message("2"));
- RxMessageBrokerMinimod.Default.Send(new Message("3"));
- }
+ public Form1()
+ {
+ InitializeComponent();
+
+ //RxMessageBrokerMinimod.Default.Register<CreateDisplayMessage>(CreateDisplayMessage, new ControlScheduler(this));
+ RxMessageBrokerMinimod.Default.Register<Start>(Start, TaskPoolScheduler.Default);
+
+ RxMessageBrokerMinimod.Default.Stream
+ .OfType<Message>()
+ .ObserveOn(new ControlScheduler(this))
+
+ .Do(message =>
+ {
+ //Alternative könnte hier auch ein Select auf CreateDisplayMessage(...) gemacht werden
+ message.DisplayMessage = CreateDisplayMessage(message);
+ })
+ .Window(1)
+ .Do(x => x
+ .Delay(TimeSpan.FromSeconds(5))
+ .ObserveOn(new ControlScheduler(this))
+ .Do(p =>
+ {
+ Controls.Remove(p.DisplayMessage);
+ p.DisplayMessage.Dispose();
+ })
+ .Subscribe())
+ .Subscribe();
+ }
+
+ void Start(Start obj)
+ {
+ RxMessageBrokerMinimod.Default.Send(new Message("1"));
+ RxMessageBrokerMinimod.Default.Send(new Message("2"));
+ RxMessageBrokerMinimod.Default.Send(new Message("3"));
+ }
- void Message(Message message)
- {
- Debug.WriteLine(DateTimeOffset.Now.Ticks + " > " + message.Value);
+ Control CreateDisplayMessage(Message message)
+ {
+ Debug.WriteLine(DateTimeOffset.Now.Ticks + " > " + message.Value);
- var label = CreateLabel();
+ var label = CreateLabel();
- label.Text = message.Value;
+ label.Text = message.Value;
- label.Size = label.GetPreferredSize(Size);
- if (label.Width < ClientRectangle.Width)
- {
- label.Width += 15;
- }
- label.Location = new Point(1, ClientRectangle.Height - label.Height - 1 - _offset);
- _offset += 15;
- label.Visible = true;
- Controls.Add(label);
- Controls.SetChildIndex(label, 0);
+ label.Size = label.GetPreferredSize(Size);
+ if (label.Width < ClientRectangle.Width)
+ {
+ label.Width += 15;
+ }
+ label.Location = new Point(1, ClientRectangle.Height - label.Height - 1 - _offset);
+ _offset += 15;
+ label.Visible = true;
+ Controls.Add(label);
+ Controls.SetChildIndex(label, 0);
- RemoveOnTimeoutOrNextMessage(label);
+ Debug.WriteLine(DateTimeOffset.Now.Ticks + " < " + message.Value);
+ return label;
+ }
- Debug.WriteLine(DateTimeOffset.Now.Ticks + " < " + message.Value);
- }
+ void button1_Click(object sender, EventArgs e)
+ {
+ RxMessageBrokerMinimod.Default.Send(new Start());
+ }
- void RemoveOnTimeoutOrNextMessage(Control control)
- {
- const long ItDoesNotMatter = 42L;
-
- var timeout = Observable.Timer(TimeSpan.FromSeconds(5));
- var nextMessage = RxMessageBrokerMinimod.Default.Stream
- .OfType<Message>()
- .Select(_ => ItDoesNotMatter)
- .FirstAsync();
-
- nextMessage
- .Amb(timeout)
- .ObserveOn(this)
- .Do(_ =>
+ static Label CreateLabel()
{
- Controls.Remove(control);
- control.Dispose();
- })
- .Subscribe();
+ return new Label
+ {
+ Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
+ Name = "message" + Guid.NewGuid(),
+ Padding = new Padding(2),
+ TextAlign = ContentAlignment.BottomLeft,
+ UseMnemonic = false,
+ Visible = false
+ };
+ }
}
- void button1_Click(object sender, EventArgs e)
+ class Start
{
- RxMessageBrokerMinimod.Default.Send(new Start());
}
- static Label CreateLabel()
+ class Message
{
- return new Label
- {
- Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
- Name = "message" + Guid.NewGuid(),
- Padding = new Padding(2),
- TextAlign = ContentAlignment.BottomLeft,
- UseMnemonic = false,
- Visible = false
- };
- }
- }
-
- class Start
- {
- }
+ public Message(string value)
+ {
+ Value = value;
+ }
- class Message
- {
- public Message(string value)
- {
- Value = value;
+ public string Value { get; set; }
+ public Control DisplayMessage { get; set; }
}
-
- public string Value { get; set; }
- }
-}
+}

0 comments on commit 4c8078f

Please sign in to comment.
Something went wrong with that request. Please try again.