もちろん使ったことなんてないっす。
なので、一から調べようと思ったんですが、ドキュメント があるわあるわ。
まず、MessageQueue を使ってプログラム遊びをしたい場合は、
ワタシの環境(XPSP2)ですが、[Windows コンポーネント] の [メッセージ キュー]を入れねばなりませんでした。
あとは参考文献を読んで、とりあえず組んでみたっす。
■参考文献
メッセージング コンポーネントの使用
MessageQueue クラス
Message クラス
■実行画像
自分で送って自分で受信してるから何も面白くないけど、一応 MessageQueue を使ってます。
Public Class MessageQueueTest
Private Const MQ_PATH As String = ".\Private$\MyQueue"
Private Const ASYNC_MQ_PATH As String = ".\Private$\MyAsyncQueue"
Private Const ASYNC_CALLBACK_MQ_PATH As String = ".\Private$\MyAsyncCallBackQueue"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' 受信結果表示領域のクリア
Me.PictureBox1.Image = Nothing
Me.DataGridView1.DataSource = Nothing
Me.DataGridView1.DataMember = Nothing
'' 専用キューの作成(取得)
Me.MessageQueue1 = Me.GetMessageQueue(MQ_PATH)
' 受診メッセージを履歴キューに残す
Me.MessageQueue1.UseJournalQueue = True
'' メッセージの送信(テキスト)
Dim textMessage As System.Messaging.Message = _
New System.Messaging.Message()
Dim xmlFormatter As System.Messaging.IMessageFormatter = _
New System.Messaging.XmlMessageFormatter(New Type() {GetType(String)})
With textMessage
.Label = "textTest"
.Body = "健康だとごはんがうまい"
.Formatter = xmlFormatter
End With
Me.MessageQueue1.Send(textMessage)
'' 受信(テキスト)
Me.MessageQueue1.Formatter = xmlFormatter
Dim receiveTextMessage As System.Messaging.Message = _
Me.MessageQueue1.Receive()
Me.Label1.Text = DirectCast(receiveTextMessage.Body, String)
'' メッセージの送信(画像)
Dim imageMessage As System.Messaging.Message = _
New System.Messaging.Message()
Dim binaryFormatter As System.Messaging.IMessageFormatter = _
New System.Messaging.BinaryMessageFormatter()
With imageMessage
.Label = "中さん"
.Body = My.Resources.中さん
.Formatter = binaryFormatter
.Priority = Messaging.MessagePriority.Highest ' 最優先のメッセージ
End With
Me.MessageQueue1.Send(imageMessage)
'' 受信(画像)
Me.MessageQueue1.Formatter = binaryFormatter
Dim receiveImageMessage As System.Messaging.Message = _
Me.MessageQueue1.Receive()
Dim receive中さん As Bitmap = DirectCast(receiveImageMessage.Body, Bitmap)
Me.PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
Me.PictureBox1.Size = New Size(CInt(receive中さん.Width / 2), CInt(receive中さん.Height / 2))
Me.PictureBox1.Image = receive中さん
'' メッセージの送信(Dataset)
Dim datasetMessage As System.Messaging.Message = _
New System.Messaging.Message()
With datasetMessage
.Label = "DatasetTest"
.Body = Me.CreateDataSet()
.Formatter = xmlFormatter
End With
Me.MessageQueue1.Send(datasetMessage)
'' 受信(Dataset)
Me.MessageQueue1.Formatter = xmlFormatter
Dim receiveDatasetMessage As System.Messaging.Message = _
Me.MessageQueue1.Receive()
Dim receiveDataset As DataSet = DirectCast(receiveDatasetMessage.Body, DataSet)
Me.DataGridView1.DataSource = receiveDataset
Me.DataGridView1.DataMember = receiveDataset.Tables(0).TableName
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not System.Messaging.MessageQueue.Exists(MQ_PATH) Then Return
' .\Private$\MyNewQueue にあるメッセージを削除
Me.MessageQueue1.Purge()
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
If Not System.Messaging.MessageQueue.Exists(MQ_PATH) Then Return
' Queue を削除
System.Messaging.MessageQueue.Delete(MQ_PATH)
End Sub
'' 非同期通信
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'' 専用キューの作成(取得)
Me.MessageQueue2 = Me.GetMessageQueue(ASYNC_MQ_PATH)
AddHandler Me.MessageQueue2.ReceiveCompleted, AddressOf ReceiveCompleted
Me.MessageQueue2.BeginReceive()
' 送信
Dim textMessage As System.Messaging.Message = _
New System.Messaging.Message()
With textMessage
.Label = "textTest2"
.Body = "病気だとごはんがまずい"
.Formatter = New System.Messaging.XmlMessageFormatter(New Type() {GetType(String)})
End With
Me.MessageQueue2.Send(textMessage)
End Sub
'' 非同期で結果を受け取る
Private Sub ReceiveCompleted(ByVal sender As Object, ByVal e As System.Messaging.ReceiveCompletedEventArgs)
Dim mq As System.Messaging.MessageQueue = _
DirectCast(sender, System.Messaging.MessageQueue)
Dim msg As System.Messaging.Message = mq.EndReceive(e.AsyncResult)
msg.Formatter = New System.Messaging.XmlMessageFormatter(New Type() {GetType(String)})
Console.WriteLine("ReceiveCompleted:" & DirectCast(msg.Body, String))
' 続けてメッセージを受信し続ける場合
mq.BeginReceive()
End Sub
'' 非同期通信 その2
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'' 専用キューの作成(取得)
Me.MessageQueue3 = Me.GetMessageQueue(ASYNC_CALLBACK_MQ_PATH)
Dim datasetMessage As System.Messaging.Message = _
New System.Messaging.Message()
With datasetMessage
.Label = "DatasetTest2"
.Body = Me.CreateDataSet()
.Formatter = New System.Messaging.XmlMessageFormatter(New Type() {GetType(DataSet)})
End With
Me.MessageQueue3.Send(datasetMessage)
' 受信開始
Me.MessageQueue3.BeginReceive(New TimeSpan(0, 0, 0, 10), Nothing, _
AddressOf CallBack)
End Sub
' 受信完了
Private Sub CallBack(ByVal result As System.IAsyncResult)
If Not result.IsCompleted Then
Console.WriteLine("not completed")
Return
End If
Me.MessageQueue3.Formatter = New System.Messaging.XmlMessageFormatter(New Type() {GetType(DataSet)})
Dim message As System.Messaging.Message = _
Me.MessageQueue3.EndReceive(result)
Dim ds As DataSet = DirectCast(message.Body, DataSet)
Console.WriteLine("DatasetName:" & ds.DataSetName)
Console.WriteLine("TableName:" & ds.Tables(0).TableName)
End Sub
Private Function GetMessageQueue(ByVal queueName As String) As System.Messaging.MessageQueue
If System.Messaging.MessageQueue.Exists(queueName) Then
' 指定された Queue が存在する場合取得
Return New System.Messaging.MessageQueue(queueName)
Else
' 指定された Queue が存在しない場合作成
Return System.Messaging.MessageQueue.Create(queueName)
End If
End Function
Private Function CreateDataSet() As DataSet
Dim ds As DataSet = New DataSet("TestDataset")
Dim dt As DataTable = New DataTable("Dogs")
ds.Tables.Add(dt)
Dim uniqueColumn As DataColumn = New DataColumn("ID", GetType(Integer))
uniqueColumn.Unique = True
uniqueColumn.AutoIncrement = True
dt.Columns.Add(uniqueColumn)
dt.Columns.Add("NAME", GetType(String))
Dim names As String() = {"ふじこ", "えでん", "オット"}
For Each name As String In names
Dim row As DataRow = dt.NewRow()
row("NAME") = name
dt.Rows.Add(row)
Next
Return ds
End Function
End Class