Using MemBus for messaging between application components
Sometimes we need publisher/subscriber messaging in our applications to broadcast messages to different parts of system in real time. We can always build our own solution for this but we can also use something that is already there. In this posting I will show you how to use MemBus to send messages from MDI parent form to MDI child forms.
NB! You can download and browse source code of this post from my GitHub samples repository.
What is MemBus?
As we can read from MemBus introduction:
“it is messaging framework … that utilizes the semantics of sending and receiving messages. Those messages are not meant to leave the AppDomain where the Bus lives in. There is no durability – when the AppDomain is gone, the Bus is gone.”
So it seems like we have interface to send and receive messages between our system components. Now let’s build something to demonstrate how to use MemBus for something useful.
Design draft
Before going to details let’s take a look at my design draft. This is just drawing that gives you idea about logical parts of system we are building. How we implement our application is another topic. But this is the concept we will try to follow.
We need something that receives events and lets MDI parent window know that there is new data. MDI parent window constructs message and sends it to Bus. Bus has subscribers that are implemented as MDI child windows. These windows implementIObserver interface. When new child window is opened then it is also registered as subscriber to bus. When child window is closed then it is unregistered. So, there is nothing complex.
Sample application
Before sending and receiving messages we need some type. Well, string is the simplest one but let’s write some more interesting application that we can extend to cool demo. So, instead of simple types I will use my own class called GeoLocationItem. The class is here.
public class GeoLocationItem
{
public string Title { get; set; }
public DateTime Time { get; set; }
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
}
Now we need event receiver. It is possible to build some more complex receiver but let’s try to make something more primitive that doesn’t need much code and maintenance. I will use Timer object on my MDI parent form. After every three seconds it sends new message to bus.
private void LocationTimerTick(object sender, EventArgs e)
{
var item = new GeoLocationItem();
item.Time = DateTime.Now;
var secondString = item.Time.Second.ToString();
item.Title = "Car " + secondString[secondString.Length - 1];
item.Longitude = item.Time.Second;
item.Latitude = item.Time.Millisecond;
_bus.Publish(item);
}
This code creates GeoLocationItem with some random data and it simulates how to track cars on their tracks.
We have also IBus instance defined in form scope.
public partial class MainForm : Form
{
private readonly IBus _bus;
public MainForm()
{
InitializeComponent();
_bus = BusSetup.StartWith<Fast>().Construct();
}
// more code here
}
When new MDI child window is opened then it is registered also with bus. When child window is closed it is also removed from bus so it does not receive events anymore.
private void NewWindowToolStripMenuItemClick(object sender, EventArgs e)
{
var child = new ChildForm {MdiParent = this};
var observable = _bus.Observe<GeoLocationItem>();
observable.Subscribe(child);
child.Tag = observable;
child.FormClosed += ChildFormClosed;
child.Show();
}
static void ChildFormClosed(object sender, FormClosedEventArgs e)
{
var form = (Form)sender;
var observable = form.Tag as IObservable<GeoLocationItem>;
if (observable != null)
{
observable.Subscribe(null);
}
}
The tricky part here is how form is introduced to bus. I ask new observable object from bus and then register my child window as subscriber to it. When child window is closed then I subscribe null as observer and bus stops sending messages to this instance. If you don’t unsubscribe then form is not destroyed and receives messages background. You may not want it.
Here is the child form. It implements IObserver interface and when new messages is received then it sends out event that is invoked in form’s own thread. Otherwise we get errors and there is no feedback shown on child forms.
public partial class ChildForm : Form, IObserver<GeoLocationItem>
{
private delegate void AddDataItemDelegate(GeoLocationItem item);
private readonly AddDataItemDelegate _addDataItem;
public ChildForm()
{
InitializeComponent();
_addDataItem = new AddDataItemDelegate(SetValue);
}
public void OnNext(GeoLocationItem value)
{
Invoke(_addDataItem, new object[] { value });
}
private void SetValue(GeoLocationItem value)
{
var item = new ListViewItem();
item.Text = value.Time.ToString();
item.SubItems.Add(value.Title);
item.SubItems.Add(value.Latitude.ToString());
item.SubItems.Add(value.Longitude.ToString());
BusDataList.Items.Insert(0, item);
}
public void OnError(Exception error)
{
throw new NotImplementedException();
}
public void OnCompleted()
{
}
}
Now let’s try to run our program.
And here is the result
I opened some windows and tiled them in MDI parent. Here is the example of program.
Okay, that’s it for now. You can see that all my eight child forms are receiving messages from parent through MemBus and this was our goal.
Conclusion
Although we can create our own messaging solutions where some clients are publishers and other are subscribers of messages we can avoid this task and use existing solutions. In this posting I demonstrated how to use MemBus to organize communication between MDI parent and child forms. We used nice standardized interface without any bad hacks and we got clean and working solution.
Nice article, thanks for sharing…
Thanks for feedback, Frank! :)
This example is my first solution that started to work with minimal set of knowledge about MemBus. I plan to go on and optimize the code so it is shorter and also easier to read. I will try out Rx Components as soon I get some time and I will make some modifications to source code of this sample project.
Thanks for feedback, Sean! I will try your messenger out for sure. :)
gunnarpeipman.com
gunnarpeipman.com
https://jekyll.s3.us-east-005.backblazeb2.com/20241101-4/research/je-tall-sf-marketing-(420).html
Jovani Plus dimension mom of the bride dresses fits any body
type.
https://jekyll.s3.us-east-005.backblazeb2.com/20250909-15/research/je-marketing-(217).html
This will help her discover the complementary
ensemble and prevent her from being over or underdressed.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-716/research/je-marketing-(232).html
Then, let the formality, season and venue of the wedding
be your guide.
https://je-tall-marketing-871.fra1.digitaloceanspaces.com/research/je-marketing-(127).html
Opt for a well-cut, flowing sundress in a breathable fabric—and pair
with dressy sandals to tug the look collectively (while still feeling comfortable on the sand!).
https://jekyll.s3.us-east-005.backblazeb2.com/20250904-2/research/je-marketing-(122).html
You do not have to purchase a mother-of-the-bride costume if the considered it
sitting untouched in your closet after the large day is unappealing.
https://storage.googleapis.com/digi461sa/research/digi461sa-(99).html
This mother of the bride donned a beautiful gentle grey gown with an illusion neckline brimming with stunning beaded detailing.
https://je-tall-marketing-875.ams3.digitaloceanspaces.com/research/je-marketing-(154).html
This desert colored gown is perfect if what the bride desires is for
you to put on a shade nearer to white.
https://jekyll.s3.us-east-005.backblazeb2.com/20250904-11/research/je-marketing-(339).html
“I stated right then I would put on it in the future,” she recalled.
https://je-tall-marketing-883.ams3.digitaloceanspaces.com/research/je-marketing-(115).html
Embroidery artfully positioned in vertical strains elongates a woman’s silhouette, making mothers look taller,
longer, and leaner.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-737/research/je-marketing-(325).html
You can present a little bit of cleavage, however an extreme quantity
of can appear a bit inappropriate.
https://digi65sa.sfo3.digitaloceanspaces.com/research/digi65sa-(218).html
Celebrate their huge day in type with our Mother of the Bride or Groom outfits.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-720/research/je-marketing-(304).html
This funky floral print mother-of-the-bride dress gives us all the ’70s vibes .
https://storage.googleapis.com/digi463sa/research/digi463sa-(185).html
Check out our options for petite mom of the bride dresses!
https://jekyll.s3.us-east-005.backblazeb2.com/20250909-1/research/monday-1-(63).html
This will complete your outfit and convey it together as a
complete.
https://jekyll.s3.us-east-005.backblazeb2.com/20250909-7/research/je-marketing-(366).html
Current popular trends characteristic lace, prints, fashionable empire
waistlines and jackets.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-736/research/je-marketing-(342).html
The fall colours are often earthy and on the darker aspect, however
always ask your daughter what she has in mind.
https://jekyll.s3.us-east-005.backblazeb2.com/20250904-1/research/je-marketing-(280).html
Avoid flashy shades like shiny pink, pink or yellow because
the gown could stand out an excessive amount of.
https://je-tall-marketing-835.lon1.digitaloceanspaces.com/research/je-marketing-(155).html
Think in regards to the colors you feel best in and the kinds of outfits that make
you shine.
https://je-tall-marketing-826.lon1.digitaloceanspaces.com/research/je-marketing-(142).html
This mom wore a standard hanbokwith floral and geometric embroidery to her daughter’s California ranch wedding ceremony.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-700/research/je-marketing-(263).html
Today’s mother of the bride collections consist of figure-flattering frocks which would possibly
be designed to accentuate your mum’s best bits.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-746/research/je-marketing-(30).html
Fall and winter weddings name for stylish muted tones like silvery grey.
https://je-tall-marketing-881.ams3.digitaloceanspaces.com/research/je-marketing-(429).html
If the marriage is outdoor or on the beach, then there will
most likely be more leeway depending on the temperature and setting.
https://storage.googleapis.com/digi470sa/research/digi470sa-(431).html
Otherwise, photos will appear off-balanced, and it might be misconstrued that one mother is
trying to outshine or outdo the other.
https://je-sf-tall-marketing-739.b-cdn.net/research/je-marketing-(463).html
It has over one hundred forty constructive reviews, many
from ladies who wore this to a marriage and loved it!
https://jekyll.s3.us-east-005.backblazeb2.com/20250904-10/research/je-marketing-(190).html
It’s important to guarantee that any head pieces don’t swamp your frame,
and doesn’t hide your face.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-721/research/je-marketing-(127).html
You’ll simply glow in this shimmering gold gown by Aidan Mattox.
https://storage.googleapis.com/digi471sa/research/digi471sa-(70).html
I can assure you that I won’t ever make that mistake once more.
https://je-tall-marketing-828.fra1.digitaloceanspaces.com/research/je-marketing-(195).html
MISSMAY creates beautiful classic style clothes that
can be worn again and again in virtually any setting.
https://je-tall-marketing-838.lon1.digitaloceanspaces.com/research/je-marketing-(325).html
If full skirts and punchy prints aren’t your mother’s go-to, strive a statement sleeve.
https://je-tall-marketing-872.ams3.digitaloceanspaces.com/research/je-marketing-(398).html
Florals set on black or dark backgrounds feel no less romantic however actually convey
the delightfully unexpected.
https://jekyll.s3.us-east-005.backblazeb2.com/20250909-5/research/je-marketing-(125).html
Ahead, 25 mother-of-the-bride appears that feel fashion-forward,
elegant, and of-the-moment for a return to weddings later this 12 months and into
2022.
https://storage.googleapis.com/digi471sa/research/digi471sa-(209).html
Jovani presents you the top highest quality MOB robes for a
low price.
https://jekyll.s3.us-east-005.backblazeb2.com/20250909-9/research/je-marketing-(318).html
A mother is a ray of shine in a daughter’s life, and so
she deserves to get all glitzy and gleamy in a sequin MOB gown.
https://storage.googleapis.com/digi471sa/research/digi471sa-(410).html
This is one component of the dresses that don’t have to
match, so lengthy as the formality is coordinated.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-718/research/je-marketing-(182).html
It’s essential to ensure that any head pieces don’t swamp your body, and doesn’t disguise your face.
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-742/research/je-marketing-(30).html
Frumpy, shapeless mom of the bride clothes are a factor of the past!