zeromq / netmq

A 100% native C# implementation of ZeroMQ for .NET
Other
2.95k stars 743 forks source link

Publisher in Windows Forms does not publish messages #1048

Closed MrSzymonello closed 1 year ago

MrSzymonello commented 1 year ago

Environment

NetMQ Version:    4.0.1.10
Operating System: Windows 10 Pro, 22H2, 19045.2364
.NET Version:     .NETFramework v4.8

Expected behaviour

The Pub/Sub model works when Publisher is a Windows Forms application.

Actual behaviour

Messages published from Windows Forms application do not reach the Subscriber.

Steps to reproduce the behaviour

Clone repository https://github.com/MrSzymonello/NetMqPubSubConsoleVsWinForms The repository contains 3 projects: Subscriber, Console Application Publisher and Windows Forms Publisher. Run Subscriber and Console Publisher. It works. Run Subscriber and Windows Forms Publisher. It does not work. Messages do not reach the Subscriber.

MrSzymonello commented 1 year ago

Here is the fix. The Form1.cs should like that

using System;
using System.Windows.Forms;
using NetMQ;
using NetMQ.Sockets;

namespace PublisherWindowsFormsApp
{
    public partial class Form1 : Form
    {
        private readonly PublisherSocket publisher = new PublisherSocket();
        public Form1()
        {
            publisher.Bind("tcp://127.0.0.1:8888");
            InitializeComponent();
        }

        private void buttonPublish_Click(object sender, EventArgs e)
        {
            publisher
                    .SendMoreFrame("COMMAND")
                    .SendFrame(textBoxCommand.Text);
        }
    }
}

not like that

using System;
using System.Windows.Forms;
using NetMQ;
using NetMQ.Sockets;

namespace PublisherWindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonPublish_Click(object sender, EventArgs e)
        {
            using (var publisher = new PublisherSocket())
            {
                publisher.Bind("tcp://127.0.0.1:8888");

                publisher
                    .SendMoreFrame("COMMAND")
                    .SendFrame(textBoxCommand.Text);
            }
        }
    }
}