public async Task Test_Schedule() { var hub = new MetaPubSub(); bool received = false; Task Handler(MyMessage x) { received = true; return(Task.CompletedTask); } hub.Subscribe <MyMessage>(Handler); var message = new MyMessage { LogSeverity = MetaLogErrorSeverity.Info, DeliverAtLeastOnce = true, WaitForSubscriberTimeout = 100 }; hub.Schedule(message, 100); await Task.Delay(50); Assert.IsFalse(received); GC.Collect(); await Task.Delay(60); Assert.IsTrue(received); }
// scheduling a message - your message can be queued and published after a time delay public async Task ScheduleExample() { var hub = new MetaPubSub(); hub.Subscribe <MyMessage>(OnMyMessage); var message = new MyMessage { DeliverAtLeastOnce = true, WaitForSubscriberTimeout = 1500 }; // The message will be published after 3 seconds delay and after that, it can wait another 500 ms for a subscriber. // When using Schedule method there is no way to receive NoSubscriberException or AggregateException. hub.Schedule(message, millisecondsDelay: 3000); Console.WriteLine($"Message scheduled at {DateTime.Now:HH:mm:ss.fff}, delay - 3 sec"); // waiting before unsubscribing await Task.Delay(3500); await hub.Unsubscribe <MyMessage>(OnMyMessage); }