主婦と.NETと犬のぶろぐ

奥様 と プログラム と お犬様 の 楽しい日常

目次

Blog 利用状況

ニュース

書庫

日記カテゴリ

MessageQueue(System.Messaging.MessageQueue)

もちろん使ったことなんてないっす。
なので、一から調べようと思ったんですが、ドキュメント があるわあるわ。

まず、MessageQueue を使ってプログラム遊びをしたい場合は、
ワタシの環境(XPSP2)ですが、[Windows コンポーネント] の [メッセージ キュー]を入れねばなりませんでした。
メッセージ キュー

あとは参考文献を読んで、とりあえず組んでみたっす。

■参考文献
メッセージング コンポーネントの使用
MessageQueue クラス
Message クラス

■実行画像
自分で送って自分で受信してるから何も面白くないけど、一応 MessageQueue を使ってます。 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

投稿日時 : 2007年2月23日 9:49

Feedback

# re: MessageQueue(System.Messaging.MessageQueue) 2007/02/23 16:13 黒龍

やってみると意外と簡単なんですよね。信頼性も高くなるしあれこれ悩まずにすむので最近お気に入りです。

# re: MessageQueue(System.Messaging.MessageQueue) 2007/02/26 10:18 なおこ(・∀・)

>> 黒龍さん
たしかに、やってみると意外に簡単に実装できました。
まぁやってることが凄く簡単なことだからかもしれませんが...

# TEoswfKcgM 2014/08/28 5:12 http://crorkz.com/

86IHyW Thanks for the blog post, can I set it up so I get an email sent to me whenever you make a new article?

# IKCgrOTdiW 2014/09/16 18:27 https://www.youtube.com/watch?v=6BlpsZ7LHRQ

This site can be a walk-by means of for all of the information you wished about this and didn't know who to ask. Glimpse here, and you'll undoubtedly discover it.

# OZQofOgPKKvTBhSVg 2014/09/18 17:02 http://kickstarter-dymaxion.info/story/27299

gSXUNN Looking forward to reading more. Great blog.Really looking forward to read more. Fantastic.

# オメガコピー 2017/08/24 5:03 ranigaslam@excite.co.jp

新品の財布の
●株式情報はいつでも更新します!
◆品質が良くて、は価格が、実物の写真が低いです!
◆経営方針:品質を重視して、納期も厳守して、第1を使いを信じます!
◆超とりわけ安い価格で、安心して、迅速で、確かで、顧客の身辺まで(に)引き渡します。
豊富な商品でそろってい最も新作もいつでも商品が到着してゆっくりと見てくださいのためです。
広大な客がご光臨賜りを歓迎します
オメガコピー http://www.gooshop001.com

タイトル  
名前  
Url
コメント