MessageQueue

Сегодня на обед MessageQueue. ) Хорошая штука, через которую, ваши приложения могут общаться друг с другом.
Создадим очень простую форму для отправки сообщения и приемки соответственно.


На кнопку вешаем:

 Public Sub SendMessage(ByVal saysmth As Object)

        ' Connect to a queue on the local computer.
        Dim myQueue As MessageQueue
        If Not MessageQueue.Exists(".\myQueue") Then
            myQueue = MessageQueue.Create(".\myQueue")
        Else
            myQueue = New MessageQueue(".\myQueue")
        End If

        Dim _obj As New SendObj
        _obj.obj = saysmth
        ' Send the Order to the queue.
        myQueue.Send(_obj)

    End Sub 'SendMessage

После нажатия кнопки вот что происходт в windows:

Создается очередь myqueue с вашим сообщением в сериализованном виде, то есть XML.
После прочтения, сообщение стирается.


Public Function ReceiveMessage() As Object
        ' Connect to the a queue on the local computer.
        Dim myQueue As New MessageQueue(".\myQueue")
        ' Set the formatter to indicate the body contains an Order.
        myQueue.Formatter = New XmlMessageFormatter(New Type() _
            {GetType(SendObj)})

        Try
            ' Receive and format the message. 
            Dim myMessage As Message = myQueue.Receive()
            Return CType(myMessage.Body, SendObj).obj

        Catch m As MessageQueueException
        Catch e As InvalidOperationException
            ' Handle invalid serialization format.
            Console.WriteLine(e.Message)
        End Try

    End Function 'ReceiveMessage

Так например вы можете заставить вашу программу проснуться, или сменить статус и многое другое. Еще много интересных свойств имеет класс MessageQueue, советую прочитать.
ссылка на статью
Спасибо.

Добавить комментарий

Loading