1:创建队列
public void CreateQueue(string queuePath)
{
try
{
if (!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(str);
MessageBox.Show("创建队列成功!");
}
else
{
MessageBox.Show(queuePath + "已经存在");
}
}
catch (MessageQueueException e)
{
MessageBox.Show(e.Message);
}
}
远程创建
public static void CreatQuerry()
{
try
{
System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(@"FormatName:DIRECT=TCP:222.18.158.119\private$\test");
if (mq != null)
{
MessageBox.Show("创建成功");
}
}
catch (MessageQueueException e)
{
Console.WriteLine(e.Message);
}
}
接受TextBox传送的值
private void btnCreate_Click(object sender, EventArgs e)
{
string str = @".\private$\" + createText.Text;
CreateQueue(str);
}
public void CreateQueue(string queuePath)
{
try
{
if (!MessageQueue.Exists(queuePath))
{
MessageQueue.Create(queuePath);
MessageBox.Show("创建队列成功!");
}
else
{
MessageBox.Show(queuePath + "已经存在");
}
}
catch (MessageQueueException e)
{
MessageBox.Show(e.Message);
}
}
发送消息:
public static bool SendMessage(Book book)
{
bool flag = false;
try
{
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(".\\private$\\newMyQueue");
System.Messaging.Message myMessage = new System.Messaging.Message();
myMessage.Body = book;
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.App.Book) });
//发送消息到队列中
myQueue.Send(myMessage);
flag = true;
}
catch (ArgumentException e)
{
MessageBox.Show(e.Message);
}
return flag;
}
public static void SendMsg()
{
try
{
System.Messaging.MessageQueue Queue;
Queue = new System.Messaging.MessageQueue(@"FormatName:DIRECT=TCP:192.168.0.101\private$\test");
System.Messaging.Message Msg = new System.Messaging.Message();
Msg.Formatter = new System.Messaging.BinaryMessageFormatter();
Msg.Body = "My test";
Queue.Send(Msg);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
private void sendMessage_Click(object sender, EventArgs e)
{
//MessageQueue msg = new MessageQueue(str);
System.Messaging.Message theMessage = new System.Messaging.Message();
theMessage.Body = txtMessage.Text;
theMessage.Label = txtName.Text;
if (highPriority.Checked)
theMessage.Priority = System.Messaging.MessagePriority.Highest;
else
theMessage.Priority = System.Messaging.MessagePriority.Normal;
helpRequestQueue.Send(theMessage);
DisplayMessages();
}
接受消息:
public static string GetMsg()
{
try
{
System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(@".\private$\test");
// Set the queue'ss formatter to decode Point objects
mq.Formatter = new System.Messaging.BinaryMessageFormatter();
System.Messaging.Trustee trustee = new System.Messaging.Trustee();
System.Messaging.Message msg = mq.Peek(new TimeSpan(1000));
// Convert received message to object that we think was sent
string pt = (string)msg.Body;
// Display it to the user
return pt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
public static string ReceiveMessage()
{
//连接到本地队列
MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.App.Book) });
try
{
//从队列中接收消息
System.Messaging.Message myMessage = myQueue.Receive();
Book book = (Book)myMessage.Body; //获取消息的内容
return string.Format("编号:{0},书名:{1},作者:{2},定价:{3}",
book.BookId,
book.BookName,
book.BookAuthor,
book.BookPrice);
}
catch (MessageQueueException e)
{
MessageBox.Show(e.Message);
}
catch (InvalidCastException e)
{
MessageBox.Show(e.Message);
}
return null;
}