tags:

views:

41

answers:

1

Following the is the code I have for listening to messages from Windows form.

I have noticed that when I click on send it sends a message to MyQueue but at that time I was hoping the event mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e) should get called but it is not, in other words I am trying to subscribe to MyQueue from Windows form. Just wondering if I am missing something in the code:

public class Form1 : System.Windows.Forms.Form
{
   public System.Messaging.MessageQueue mq;
   public static Int32 j=0;

   public Form1()
   {
       // Required for Windows Form Designer support
       InitializeComponent();

       // Queue Creation
       if(MessageQueue.Exists(@".\Private$\MyQueue"))
         mq = new System.Messaging.MessageQueue(@".\Private$\MyQueue");
       else
         mq = MessageQueue.Create(@".\Private$\MyQueue");

       mq.ReceiveCompleted += new ReceiveCompletedEventHandler(mq_ReceiveCompleted);
       mq.BeginReceive();
   }

   [STAThread]
   static void Main() 
   {
       Application.Run(new Form1());
   }

   private void btnMsg_Click(object sender, System.EventArgs e)
   {
      // SendMessage(Handle, 1, 0, IntPtr.Zero);
      System.Messaging.Message mm = new System.Messaging.Message();
      mm.Body = txtMsg.Text;
      mm.Label = "Msg" + j.ToString();
      j++;
      mq.Send(mm);
   }

   void mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
   {
       //throw new NotImplementedException();
   }

   private void btnRcv_Click(object sender, System.EventArgs e)
   {
        System.Messaging.Message mes;
        string m;

        try
        {
            mes = mq.Receive(new TimeSpan(0, 0, 3));
            mes.Formatter = new XmlMessageFormatter(new String[] {"System.String,mscorlib"});
            m = mes.Body.ToString();
        }
        catch
        {
            m = "No Message";
        }
        MsgBox.Items.Add(m.ToString());
    }
}
+1  A: 

See MSDN's example on how to use the ReceiveCompletedEventHandler .

They have a console app where the Main() does the same as your Form1(), but your handler doesn't have any code. You've said it doesn't call back into your event delegate, but perhaps check your queue name is correct on the constructor.

Consider using MSDN's sample code in a new console app to test your environment first, then go back to your WinForms code with any lessons learned.

private static void MyReceiveCompleted(Object source, 
        ReceiveCompletedEventArgs asyncResult)
    {
        MessageQueue mq = (MessageQueue)source;
        Message m = mq.EndReceive(asyncResult.AsyncResult);

        Console.WriteLine("Message: " + (string)m.Body);
        mq.BeginReceive();      
        return; 
    }

If you want to inspect the queue and get a message on the click of a button, you can simply move the statement mq.BeginReceive(); to the btnRcv_Click() in place of .Receive();

p.campbell
Hi Campbell here is the modified code:It only triggers this code when the app starts up not when you click on the button.<code>static void mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e) { MessageBox.Show("Sai"); }</code>Please let me know if u have any suggestions.Thanks
np