S
Simon Hart [MVP]
You can only post messages to a remote private queue. You can't peek them, or
call the BeginReceive, Receive and register the ReceiveCompleted event
handler.
Sounds like you need to define a public queue instead of a private one.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com
call the BeginReceive, Receive and register the ReceiveCompleted event
handler.
Sounds like you need to define a public queue instead of a private one.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com
David said:Here is some code. The m_queue.BeginReceive method gets called, but
after a few seconds it throws an exception which is caught, but the ex
variable is null. My MessageArrived method is never called either even
though I have added it to the ReceiveCompleted event. Now I dont know what
to do. This is pretty darn simple code. I can see two messages in the queue
on david-1. I can run the server app on david-1 at the same time as the
remote side is running. The server app creates new entries but still the
MessageArrived handler is not called and a bogus exception is thrown.
Ideas why this does not work?
public partial class Form1 : Form
{
private MessageQueue m_queue;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
string deviceIP = String.Empty;
IPAddress[] addresses =
Dns.GetHostEntry("david-1").AddressList;
for (int i = 0; i < addresses.Length; ++i)
{
IPAddress addr = addresses;
if (addr.AddressFamily == AddressFamily.InterNetwork)
{
deviceIP = addr.ToString();
break;
}
}
if (deviceIP == String.Empty)
{
MessageBox.Show("Unable to retrieve Device IP");
Close();
return;
}
String strPath =
String.Format(@"FormatNameIRECT=TCP:{0}\PRIVATE$\CallerID", deviceIP);
m_queue = new MessageQueue(strPath);
if (m_queue != null)
{
System.Type[] types = new Type[1];
types[0] = typeof(CallerIDClient.CallerID);
m_queue.Formatter = new XmlMessageFormatter(types);
try
{
m_queue.ReceiveCompleted += new
ReceiveCompletedEventHandler(MessageArrived);
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
Close();
}
Thread t1 = new Thread(PeekMessages);
t1.IsBackground = true;
t1.Start();
}
else
{
MessageBox.Show("Queue not created");
Close();
}
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
return;
}
}
private void PeekMessages()
{
while (true)
{
try
{
m_queue.BeginReceive();
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
public void MessageArrived(Object source, ReceiveCompletedEventArgs
args)
{
MessageQueue queue = source as MessageQueue;
Message message = queue.EndReceive(args.AsyncResult);
CallerIDClient.CallerID callerid = message.Body as
CallerIDClient.CallerID;
if (callerid != null)
{
ListViewItem lvi = new ListViewItem(callerid.Name);
lvi.SubItems.Add(callerid.Number);
listView1.Items.Add(lvi);
}
}
private void AddItem(String strID)
{
Message message = m_queue.PeekById(strID);
CallerIDClient.CallerID callerid = message.Body as
CallerIDClient.CallerID;
if (callerid != null)
{
ListViewItem lvi = new ListViewItem(callerid.Name);
lvi.SubItems.Add(callerid.Number);
listView1.Items.Add(lvi);
}
}
}