Advertisement
Guest User

Sample MSMQ fails with sgen.exe

a guest
Jun 27th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.50 KB | None | 0 0
  1. Imports System.Messaging
  2. Imports System.IO
  3. Imports System.Diagnostics
  4. Imports System.ComponentModel
  5.  
  6. Public Class MainForm
  7.  
  8.     Private m_Path As String = ".\private$\orders"
  9.     Private b_Path As String = ".\private$\begin"
  10.     Public recmes As InfoMessage
  11.     Public DrinkCount As Integer = 0
  12.     Public GenCount As Integer = 0
  13.     Public listenerID As String
  14.     Public currentProcess As Process
  15.     Public WhatKindOfMessageID As String
  16.  
  17.  
  18.     ' Starts reading messages from the queue.
  19.     Private Sub HookQueue()
  20.         Try
  21.             If qOrders Is Nothing Then
  22.                 qOrders = New System.Messaging.MessageQueue(m_Path)
  23.             End If
  24.  
  25.             ' Start waiting for messages to arrive.
  26.             qOrders.BeginReceive()
  27.  
  28.         Catch exp As MessageQueueException
  29.             My.Application.Log.WriteEntry(exp.Message)
  30.         Catch exp As Exception
  31.             My.Application.Log.WriteEntry(exp.Message)
  32.         End Try
  33.     End Sub
  34.  
  35.     ' Stops reading messages from the queue.
  36.     Private Sub UnhookQueue()
  37.         Try
  38.             qOrders.Close()
  39.         Catch exp As Exception
  40.             My.Application.Log.WriteEntry(exp.Message)
  41.         End Try
  42.     End Sub
  43.  
  44.     ' Event Handler for message recieved event.
  45.     Private Sub qOrders_ReceiveCompleted(ByVal sender As Object, ByVal e As System.Messaging.ReceiveCompletedEventArgs) Handles qOrders.ReceiveCompleted
  46.         Try
  47.  
  48.             ' Set the formatter to indicate body contains an Order.
  49.             qOrders.Formatter = New XmlMessageFormatter(New Type() {GetType(InfoMessage)})
  50.  
  51.             ' Get the message.
  52.             Dim m As Message
  53.             m = qOrders.EndReceive(e.AsyncResult)
  54.  
  55.             recmes = CType(m.Body, InfoMessage)
  56.  
  57.             OrderListbox.Items.Add(m.Label)
  58.             OrderListbox.Items.Add(recmes.Fuser)
  59.             OrderListbox.Items.Add(recmes.Fpass)
  60.             OrderListbox.Items.Add(recmes.Flink)
  61.  
  62.             ' Add the message to the Listbox.
  63.             'OrderListbox.Items.Add(m.Label)
  64.             ReplyBox.Text = m.Label
  65.  
  66.             ' Now continue listening for messages.
  67.             qOrders.BeginReceive()
  68.  
  69.         Catch exp As MessageQueueException
  70.             My.Application.Log.WriteEntry(exp.Message)
  71.         Catch exp As Exception
  72.             My.Application.Log.WriteEntry(exp.Message)
  73.         End Try
  74.     End Sub
  75.  
  76.     ' Clears the Listbox.
  77.     Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
  78.         OrderListbox.Items.Clear()
  79.     End Sub
  80.  
  81.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  82.  
  83.         ' Create the private queue if it does not exist.
  84.         Try
  85.             If Not MessageQueue.Exists(".\private$\orders") Then
  86.                 MessageQueue.Create(".\private$\orders")
  87.             End If
  88.         Catch ex As Exception
  89.  
  90.         End Try
  91.  
  92.         ' Creates the START queue if it does not exist.
  93.         Try
  94.             If Not MessageQueue.Exists(".\private$\begin") Then
  95.                 MessageQueue.Create(".\private$\begin")
  96.             End If
  97.         Catch ex As Exception
  98.  
  99.         End Try
  100.  
  101.         ' Create the RETURN queue if it does not exist.
  102.         Try
  103.             If Not MessageQueue.Exists(".\private$\return") Then
  104.                 MessageQueue.Create(".\private$\return")
  105.             End If
  106.         Catch ex As Exception
  107.  
  108.         End Try
  109.  
  110.         BeginHookQueue()
  111.  
  112.         currentProcess = Process.GetCurrentProcess()
  113.  
  114.         ReturnQueue.Send("", currentProcess.Id.ToString)
  115.  
  116.  
  117.     End Sub
  118.  
  119.     ' Toggles listening for messages.
  120.     Private Sub Listen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Listen.CheckedChanged
  121.         If Listen.Checked Then
  122.             Try
  123.                 HookQueue()
  124.             Catch exp As MessageQueueException
  125.                 My.Application.Log.WriteEntry(exp.Message)
  126.             Catch exp As Exception
  127.                 My.Application.Log.WriteEntry(exp.Message)
  128.             End Try
  129.         Else
  130.             UnhookQueue()
  131.         End If
  132.     End Sub
  133.  
  134.     Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
  135.  
  136.         BeginUnhookQueue()
  137.  
  138.         End
  139.     End Sub
  140.  
  141.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  142.  
  143.         Try
  144.             ' when we are stopped.
  145.             If qOrders Is Nothing Then
  146.                 qOrders = New System.Messaging.MessageQueue(m_Path)
  147.             End If
  148.  
  149.             Dim m As Message
  150.  
  151.             ' Set the formatter to indicate body contains an Order.
  152.             qOrders.Formatter = New XmlMessageFormatter(New Type() {GetType(InfoMessage)})
  153.  
  154.             Dim TestEnum As System.Messaging.MessageEnumerator
  155.  
  156.             TestEnum = qOrders.GetMessageEnumerator2
  157.  
  158.             Dim CurMsgID As String                              
  159.             While (TestEnum.MoveNext())
  160.                 m = TestEnum.Current
  161.  
  162.                 If (m.Label = WhatKindOfMessageID) Then
  163.                     CurMsgID = TestEnum.Current.Id              
  164.                     m = qOrders.ReceiveById(CurMsgID)          
  165.                     recmes = CType(m.Body, InfoMessage)
  166.                     OrderListbox.Items.Add(m.Label)
  167.                     OrderListbox.Items.Add(recmes.Fuser)
  168.                     OrderListbox.Items.Add(recmes.Fpass)
  169.                     OrderListbox.Items.Add(recmes.Flink)
  170.  
  171.                 End If
  172.  
  173.             End While
  174.  
  175.         Catch exp As MessageQueueException
  176.             My.Application.Log.WriteEntry(exp.Message)
  177.         Catch exp As Exception
  178.             My.Application.Log.WriteEntry(exp.Message)
  179.         End Try
  180.     End Sub
  181.  
  182.     ' Starts reading messages from the begin queue.
  183.     Private Sub BeginHookQueue()
  184.         Try
  185.             ' when we are stopped.
  186.             If StartQueue Is Nothing Then
  187.                 StartQueue = New System.Messaging.MessageQueue(b_Path)
  188.             End If
  189.  
  190.             ' Start waiting for messages to arrive.
  191.             StartQueue.BeginReceive()
  192.  
  193.         Catch exp As MessageQueueException
  194.             My.Application.Log.WriteEntry(exp.Message)
  195.         Catch exp As Exception
  196.             My.Application.Log.WriteEntry(exp.Message)
  197.         End Try
  198.     End Sub
  199.  
  200.     ' Stops reading messages from the queue.
  201.     Private Sub BeginUnhookQueue()
  202.         Try
  203.             StartQueue.Close()
  204.         Catch exp As Exception
  205.             My.Application.Log.WriteEntry(exp.Message)
  206.         End Try
  207.     End Sub
  208.  
  209.     ' Event Handler for message recieved event.
  210.     Private Sub StartQueue_ReceiveCompleted(ByVal sender As Object, ByVal e As System.Messaging.ReceiveCompletedEventArgs) Handles StartQueue.ReceiveCompleted
  211.         Try
  212.  
  213.             ' Get the message.
  214.             Dim m As Message
  215.             m = StartQueue.EndReceive(e.AsyncResult)
  216.  
  217.             WhatKindOfMessageID = m.Label
  218.             MsgBox(currentProcess.Id.ToString, , "StartQ Message")
  219.  
  220.             ' Now continue listening for messages.
  221.             ' StartQueue.BeginReceive()  
  222.  
  223.         Catch exp As MessageQueueException
  224.             My.Application.Log.WriteEntry(exp.Message)
  225.         Catch exp As Exception
  226.             My.Application.Log.WriteEntry(exp.Message)
  227.         End Try
  228.     End Sub
  229.  
  230. End Class
  231.  
  232.  
  233.  
  234. Public Class InfoMessage
  235.     Public Fuser As String
  236.     Public Fpass As String
  237.     Public Flink As String
  238. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement