From fb2177d745ec431b3004a94e4f506e8d8d0b976a Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Sat, 26 Sep 2020 15:16:44 +0200 Subject: [PATCH] Esempio gestione PubSub in Redis --- TestBench/MainFOrm.cs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/TestBench/MainFOrm.cs b/TestBench/MainFOrm.cs index 6c38a33..8579b32 100644 --- a/TestBench/MainFOrm.cs +++ b/TestBench/MainFOrm.cs @@ -25,6 +25,24 @@ namespace TestBench myInitComponent(); } + private delegate void SetTextCallback(string text); + + private void SetText(string text) + { + // InvokeRequired required compares the thread ID of the + // calling thread to the thread ID of the creating thread. + // If these threads are different, it returns true. + if (this.lblChannelReceive.InvokeRequired) + { + SetTextCallback d = new SetTextCallback(SetText); + this.Invoke(d, new object[] { text }); + } + else + { + this.lblChannelReceive.Text = text; + } + } + private void myInitComponent() { // imposto folders @@ -40,11 +58,9 @@ namespace TestBench myRedis = new SteamWare.IO.Redis(); subscriber = myRedis.RedPubSub; // fix redis channel subscription - subscriber.Subscribe("TestChannel").OnMessage(async channelMessage => + subscriber.Subscribe("TestChannel").OnMessage(channelMessage => { - await Task.Delay(10); - //Console.WriteLine((string)channelMessage.Message); - lblChannelReceive.Text = (string)channelMessage.Message; + SetText((string)channelMessage.Message); }); } @@ -57,8 +73,8 @@ namespace TestBench subscriber.Publish("TestChannel", $"{DateTime.Now} | {txtChannelSend.Text}"); // reset txtChannelSend.Text = ""; + //MessageBox.Show("ENTER has been pressed!"); } - MessageBox.Show("ENTER has been pressed!"); //// ESC //else if (e.KeyChar == (char)27) // this.Close();