不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
Windows系统重启后消息队列中的消息不丢失解决方法
编辑:dnawo 日期:2011-11-10
当调用MessageQueue对象的Send方法将消息发送到队列中后,若Windows系统意外重启,默认情况下,消息队列中的消息会丢失,MS显然考虑到了这个问题,只需将Message对象的Recoverable属性设置为true,然后再将其发送到队列,这样系统重启后消息就不会丢失了。

以下代码在Windows2003下测试通过:

以下代码在Windows2003下测试通过:
复制内容到剪贴板
程序代码

using System;
using System.Messaging;
namespace ConsoleApplication1
{
class Program
{
static readonly string MQPATH = @".\private$\MzwuCom";
/// <summary>
/// 程序入口
/// </summary>
/// <param name="args">
/// 发送消息:mq.exe c hello
/// 接收消息:mq.exe r
/// </param>
static void Main(string[] args)
{
if (args.Length == 1 && args[0].ToLower() == "r")
{
string msg = MQReceive();
if (msg.Substring(0, 1) == "1")
Console.WriteLine("收到队列消息:{0}", msg.Substring(2));
else
Console.WriteLine("接收队列消息失败:{0}", msg.Substring(2));
}
else if (args.Length == 2 && args[0].ToLower() == "c")
{
if (MQSend(args[1]))
Console.WriteLine("发送消息到队列:{0}", args[1]);
else
Console.WriteLine("发送消息到队列失败!");
}
else
{
Console.WriteLine("命令行参数错误!");
}
}
/// <summary>
/// 创建消息队列
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
static MessageQueue MQCreate(string path)
{
try
{
if (!MessageQueue.Exists(path))
return MessageQueue.Create(path);
else
return new MessageQueue(path);
}
catch { }
return null;
}
/// <summary>
/// 发送消息到队列
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
static bool MQSend(string msg)
{
using (MessageQueue mq = MQCreate(MQPATH))
{
if (mq != null)
{
Message message = new Message();
message.Recoverable = true;//系统重启后消息不丢失
message.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
message.Body = msg;
mq.Send(message);
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 接收一条消息
/// </summary>
/// <returns></returns>
static string MQReceive()
{
string result = "队列中暂无消息!";
try
{
using (MessageQueue mq = MQCreate(MQPATH))
{
if (mq != null)
{
mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
using (Message msg = mq.Receive(TimeSpan.FromSeconds(5)))
{
return "1|" + msg.Body.ToString();
}
}
else
result = "创建MessageQueue对象失败!";
}
}
catch (Exception ex)
{
result = ex.Message;
}
return "0|" + result;
}
}
}
using System.Messaging;
namespace ConsoleApplication1
{
class Program
{
static readonly string MQPATH = @".\private$\MzwuCom";
/// <summary>
/// 程序入口
/// </summary>
/// <param name="args">
/// 发送消息:mq.exe c hello
/// 接收消息:mq.exe r
/// </param>
static void Main(string[] args)
{
if (args.Length == 1 && args[0].ToLower() == "r")
{
string msg = MQReceive();
if (msg.Substring(0, 1) == "1")
Console.WriteLine("收到队列消息:{0}", msg.Substring(2));
else
Console.WriteLine("接收队列消息失败:{0}", msg.Substring(2));
}
else if (args.Length == 2 && args[0].ToLower() == "c")
{
if (MQSend(args[1]))
Console.WriteLine("发送消息到队列:{0}", args[1]);
else
Console.WriteLine("发送消息到队列失败!");
}
else
{
Console.WriteLine("命令行参数错误!");
}
}
/// <summary>
/// 创建消息队列
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
static MessageQueue MQCreate(string path)
{
try
{
if (!MessageQueue.Exists(path))
return MessageQueue.Create(path);
else
return new MessageQueue(path);
}
catch { }
return null;
}
/// <summary>
/// 发送消息到队列
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
static bool MQSend(string msg)
{
using (MessageQueue mq = MQCreate(MQPATH))
{
if (mq != null)
{
Message message = new Message();
message.Recoverable = true;//系统重启后消息不丢失
message.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
message.Body = msg;
mq.Send(message);
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 接收一条消息
/// </summary>
/// <returns></returns>
static string MQReceive()
{
string result = "队列中暂无消息!";
try
{
using (MessageQueue mq = MQCreate(MQPATH))
{
if (mq != null)
{
mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
using (Message msg = mq.Receive(TimeSpan.FromSeconds(5)))
{
return "1|" + msg.Body.ToString();
}
}
else
result = "创建MessageQueue对象失败!";
}
}
catch (Exception ex)
{
result = ex.Message;
}
return "0|" + result;
}
}
}
评论: 0 | 引用: 0 | 查看次数: 8270
发表评论
请登录后再发表评论!