0

Can someone please help how to modify this code to receive only one message at a time?

    Private Sub Button_SEND_Click(sender As Object, e As EventArgs) Handles Button_SEND.Click
    Dim client As QueueClient = QueueClient.CreateFromConnectionString(My.Settings.connString, My.Settings.queueName)
    Dim message As New BrokeredMessage(TextBox_M2SEND.Text.Trim)
    client.Send(message)
    client = Nothing : message = Nothing
End Sub

' reading the message
Private Sub Button_READ_Click(sender As Object, e As EventArgs) Handles Button_READ.Click
    Dim client As QueueClient = QueueClient.CreateFromConnectionString(My.Settings.connString, My.Settings.queueName)
    Dim options As New OnMessageOptions()

    options.AutoComplete = False
    'options.AutoRenewTimeout = TimeSpan.FromMinutes(1)
    client.OnMessage(Function(message)
                         Try
                             ' Process message from subscription.
                             Console.WriteLine(vbLf & "**High Messages**")
                             Console.WriteLine("Body: " + message.GetBody(Of String)())
                             Console.WriteLine("MessageID: " + message.MessageId)
                             ' Console.WriteLine("Message Number: " + message.Properties("MessageNumber"))

                             ' Remove message from subscription.
                             message.Complete()
                         Catch generatedExceptionName As Exception
                             ' Indicates a problem, unlock message in subscription.
                             message.Abandon()
                         End Try

                     End Function, options)

    client = Nothing : options = Nothing
End Sub

I`m able to store multiple messages, but when I execute Button_Read it pulls all of them. How do I pull only one at a time?

Thank you. Alex.

evilSnobu
  • 24,582
  • 8
  • 41
  • 71
alex.s
  • 17
  • 7
  • `options.MaxConcurrentCalls = 1` BTW, creating and deleting a client on _every_ button click is not a good idea. You should create your client once and re-use it. – Sean Feldman Mar 28 '18 at 03:34

1 Answers1

1

Just call client.Receive() method to pull a single message.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • Right, that's the one. – evilSnobu Mar 27 '18 at 22:08
  • Thank you Mikhail, can do I pull the data? I assume I have to replace client.OnMessage .... with client.Receive(). How do I get the body of the message/Message Id for instance? – alex.s Mar 28 '18 at 13:13
  • @alex.s You'll get `BrokeredMessage` back, same as in `OnMessage` – Mikhail Shilkov Mar 28 '18 at 13:17
  • 1
    I think I got it. I added the following: Dim onemessage As BrokeredMessage onemessage = client.Receive() Console.WriteLine(onemessage.GetBody(Of String)) onemessage.Complete() – alex.s Mar 28 '18 at 13:23