0

Objective is click of button should start populating textbox with strings received from python script. In debug stepping I get to see the strings being received in "var message". But by virtue of while(true), the GUI freezes and one never gets to see textbox contents.

Tried Backgroundworker, but it throws exception for no access to richTextBox2.

    private void button4_Click(object sender, EventArgs e)
    {
        using (var context = ZmqContext.Create())
        {
            using (ZmqSocket SubscriberSocket = context.CreateSocket(SocketType.SUB))
            {
                SubscriberSocket.Subscribe(Encoding.UTF8.GetBytes("220.20"));
                SubscriberSocket.Connect("tcp://127.0.0.1:5000");
                while(true)
                {
                  var message = SubscriberSocket.Receive(Encoding.UTF8);
                  richTextBox2.Text += message + '\n';
                }
            }
        }
    }
slava
  • 1,901
  • 6
  • 28
  • 32
Rich
  • 43
  • 8

1 Answers1

0

It is not a clear solution, but it should work

public delegate void updateTextBoxDelegate(string textBoxString);
public updateTextBoxDelegate updateTextBox;

void updateTextBox1(string message) { richTextBox2.Text = message + '\n'; }

void UpdatetextBox(string strItem)
{
    if (richTextBox2.InvokeRequired)
    {
        this.Invoke(this.updateTextBox, strItem);
    }
}

And then update your button4_Click

private void button4_Click(object sender, EventArgs e)
    {
        using (var context = ZmqContext.Create())
        {
            using (ZmqSocket SubscriberSocket = context.CreateSocket(SocketType.SUB))
            {
                SubscriberSocket.Subscribe(Encoding.UTF8.GetBytes("220.20"));
                SubscriberSocket.Connect("tcp://127.0.0.1:5000");
                while(true)
                {
                  var message = SubscriberSocket.Receive(Encoding.UTF8);
                  UpdatetextBox(message);
                }
            }
        }
    }
slava
  • 1,901
  • 6
  • 28
  • 32
  • Thanks Slava, somehow I managed it as per http://stackoverflow.com/questions/14794393/examples-of-zeromq-pub-sub-with-c-sharp-winform/14797475#14797475 – Rich Aug 28 '14 at 07:13