using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
using System.Threading;
using System.Windows.Forms;
namespace MSMQ.Async
{
public class msgQueue
{
/// <summary>
/// ManualResetEvent
/// 通知一个或多个正在等待的线程已发生事件
/// </summary>
static ManualResetEvent signal = new ManualResetEvent(false);
/// 通过Create方法创建使用指定路径的新消息队列
/// </summary>
/// <param name="queuePath"></param>
///<summary>
/// 注意这里如果前边加个@后边就不需要考虑转移字符,如果前边不加@就需要考虑转义字符///
///如果不存在该队列,则重新创建;否则提示已经存在/////
///</summary>
public static void Createqueue(string QueuePath)
{
try
{
if (!MessageQueue.Exists(@".\private$\" + QueuePath))
{
MessageBox.Show(@".\private$\" + QueuePath);
MessageQueue.Create(@".\private$\" + QueuePath);
MessageBox.Show("创建队列成功!");
}
else
{
MessageBox.Show(QueuePath + "已经存在!");
}
}
catch (MessageQueueException e)
{
MessageBox.Show(e.Message);
}
}
/// <summary>
/// 连接消息队列并发送消息到队列
/// </summary>
public static void SendMessage(string QueuePath,string txt2,string txt3,string txt4,string txt5)
{
try
{
///连接到本地的专用队列myQueue
if (QueuePath != null && QueuePath != "")
{
MessageQueue myQueue = new MessageQueue(".\\private$\\" + QueuePath);
MessageBox.Show(myQueue.Transactional.ToString());
///判断该队列是否接受事务///
if (myQueue.Transactional)
{
Book book = new Book();
book.BookId = int.Parse(txt2);
book.BookName = txt3;
book.BookAuthor =txt4;
book.BookPrice = double.Parse(txt5); System.Messaging.Message myMessage = new System.Messaging.Message();
myMessage.Body = book; ///对发送的消息进行序列化///
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.Async.Book) });
///发送消息到队列中///
///MessageBox.Show(book.BookAuthor);
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
//启动事务
myTransaction.Begin();
//发送消息到队列中
myQueue.Send(myMessage, myTransaction);
///MessageBox.Show(myMessage.Body.ToString());
//提交事务
myTransaction.Commit(); MessageBox.Show("消息队列发送成功!!!");
}
}
else
{
MessageBox.Show("消息队列为空,请创建消息队列!!!");
}
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentException em)
{
MessageBox.Show(em.Message);
}
}
/// <summary>
/// 连接消息队列并从队列中异步接收消息
/// </summary>
public static void ReceiveMessage(string QueuePath)
{
try
{
///连接到本地的专用队列myQueue
if (QueuePath != null && QueuePath != "")
{
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(".\\private$\\" + QueuePath);
MessageBox.Show(myQueue.Transactional.ToString());
if (myQueue.Transactional)
{
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
//注意这里使用了委托,当接收消息完成的时候就执行myReceiveCompleted方法 myQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.Async.Book) });
try
{
//从队列中接收消息
myTransaction.Begin();
myQueue.BeginReceive();///注意这里启动一个没有超时时限的异步操作///
signal.WaitOne();
myTransaction.Commit();
} catch (MessageQueueException e)
{
MessageBox.Show(e.Message);
}
catch (InvalidCastException e)
{
MessageBox.Show(e.Message);
}
}
}
else
{
MessageBox.Show("消息队列为空,请创建消息队列!!!");
}
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentException em)
{
MessageBox.Show(em.Message);
}
}
public static void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
{
try
{
MessageQueue myQueue = (MessageQueue)source;
//完成指定的异步接收操作
System.Messaging.Message message = myQueue.EndReceive(asyncResult.AsyncResult);
signal.Set();
Book book = message.Body as Book;
myQueue.BeginReceive();
////Book book = (Book)MyMessage.Body;///注意这里要转换为Book类型///
Console.WriteLine("图书编号:{0}--图书名称:{1}--图书作者:{2}--图书定价:{3}",
book.BookId.ToString(),
book.BookName,
book.BookAuthor,
book.BookPrice.ToString());
}
catch (MessageQueueException me)
{
Console.WriteLine("异步接收出错,原因:" + me.Message); }
}
}
}各位帮我看看为什么
///判断该队列是否接受事务///
if (myQueue.Transactional)这儿队列不能接收事务MessageBox.Show(myQueue.Transactional.ToString());总是false
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
using System.Threading;
using System.Windows.Forms;
namespace MSMQ.Async
{
public class msgQueue
{
/// <summary>
/// ManualResetEvent
/// 通知一个或多个正在等待的线程已发生事件
/// </summary>
static ManualResetEvent signal = new ManualResetEvent(false);
/// 通过Create方法创建使用指定路径的新消息队列
/// </summary>
/// <param name="queuePath"></param>
///<summary>
/// 注意这里如果前边加个@后边就不需要考虑转移字符,如果前边不加@就需要考虑转义字符///
///如果不存在该队列,则重新创建;否则提示已经存在/////
///</summary>
public static void Createqueue(string QueuePath)
{
try
{
if (!MessageQueue.Exists(@".\private$\" + QueuePath))
{
MessageBox.Show(@".\private$\" + QueuePath);
MessageQueue.Create(@".\private$\" + QueuePath);
MessageBox.Show("创建队列成功!");
}
else
{
MessageBox.Show(QueuePath + "已经存在!");
}
}
catch (MessageQueueException e)
{
MessageBox.Show(e.Message);
}
}
/// <summary>
/// 连接消息队列并发送消息到队列
/// </summary>
public static void SendMessage(string QueuePath,string txt2,string txt3,string txt4,string txt5)
{
try
{
///连接到本地的专用队列myQueue
if (QueuePath != null && QueuePath != "")
{
MessageQueue myQueue = new MessageQueue(".\\private$\\" + QueuePath);
MessageBox.Show(myQueue.Transactional.ToString());
///判断该队列是否接受事务///
if (myQueue.Transactional)
{
Book book = new Book();
book.BookId = int.Parse(txt2);
book.BookName = txt3;
book.BookAuthor =txt4;
book.BookPrice = double.Parse(txt5); System.Messaging.Message myMessage = new System.Messaging.Message();
myMessage.Body = book; ///对发送的消息进行序列化///
myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.Async.Book) });
///发送消息到队列中///
///MessageBox.Show(book.BookAuthor);
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
//启动事务
myTransaction.Begin();
//发送消息到队列中
myQueue.Send(myMessage, myTransaction);
///MessageBox.Show(myMessage.Body.ToString());
//提交事务
myTransaction.Commit(); MessageBox.Show("消息队列发送成功!!!");
}
}
else
{
MessageBox.Show("消息队列为空,请创建消息队列!!!");
}
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentException em)
{
MessageBox.Show(em.Message);
}
}
/// <summary>
/// 连接消息队列并从队列中异步接收消息
/// </summary>
public static void ReceiveMessage(string QueuePath)
{
try
{
///连接到本地的专用队列myQueue
if (QueuePath != null && QueuePath != "")
{
//连接到本地的队列
MessageQueue myQueue = new MessageQueue(".\\private$\\" + QueuePath);
MessageBox.Show(myQueue.Transactional.ToString());
if (myQueue.Transactional)
{
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
//注意这里使用了委托,当接收消息完成的时候就执行myReceiveCompleted方法 myQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.Async.Book) });
try
{
//从队列中接收消息
myTransaction.Begin();
myQueue.BeginReceive();///注意这里启动一个没有超时时限的异步操作///
signal.WaitOne();
myTransaction.Commit();
} catch (MessageQueueException e)
{
MessageBox.Show(e.Message);
}
catch (InvalidCastException e)
{
MessageBox.Show(e.Message);
}
}
}
else
{
MessageBox.Show("消息队列为空,请创建消息队列!!!");
}
}
catch (MessageQueueException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentException em)
{
MessageBox.Show(em.Message);
}
}
public static void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
{
try
{
MessageQueue myQueue = (MessageQueue)source;
//完成指定的异步接收操作
System.Messaging.Message message = myQueue.EndReceive(asyncResult.AsyncResult);
signal.Set();
Book book = message.Body as Book;
myQueue.BeginReceive();
////Book book = (Book)MyMessage.Body;///注意这里要转换为Book类型///
Console.WriteLine("图书编号:{0}--图书名称:{1}--图书作者:{2}--图书定价:{3}",
book.BookId.ToString(),
book.BookName,
book.BookAuthor,
book.BookPrice.ToString());
}
catch (MessageQueueException me)
{
Console.WriteLine("异步接收出错,原因:" + me.Message); }
}
}
}各位帮我看看为什么
///判断该队列是否接受事务///
if (myQueue.Transactional)这儿队列不能接收事务MessageBox.Show(myQueue.Transactional.ToString());总是false
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货