【发布时间】:2018-09-29 09:42:24
【问题描述】:
我正在构建从 MSMQ(Microsoft 消息队列)读取消息的 ac# 控制台应用程序,我希望它永远运行,但运行一天后它停止从 MSMQ 读取消息,直到我右键单击它的控制台,我的问题似乎有点类似于这个:“Console Application "pausing" c#”。 Bellow是我正在使用的功能:
private static void downloadHandler(object args)
{
while (true)
{
while (CURRENT_ACTIVE_THREAD < MAX_THREAD)
{
DownloadController.WriteLog("Current active Thread = " + CURRENT_ACTIVE_THREAD);
Console.WriteLine("Current active Thread = " + CURRENT_ACTIVE_THREAD);
Thread.Sleep(1000);
MessageQueue messageQueue;
if (MessageQueue.Exists(messageQueuePath))
{
messageQueue = new MessageQueue(messageQueuePath);
Message requestMessage = messageQueue.Receive();
try
{
requestMessage.Formatter = new BinaryMessageFormatter();
string msg = requestMessage.Body.ToString();
if (!string.IsNullOrEmpty(msg))
{
DownloadController.WriteLog("received message with message = " + msg);
CURRENT_ACTIVE_THREAD += 1;
RequestDownload request = new RequestDownload();
request = JsonConvert.DeserializeObject<RequestDownload>(msg);
DownloadController.WriteLog("received message with contentId = " + request.contentId + "from message queue | title= " + request.contentTitle + " | url = " + request.baseURL);
DownloadController downloader = new DownloadController();
Thread t = new Thread(new ParameterizedThreadStart(downloader.findURLandDownload));
object[] objs = new object[2];
objs[0] = request;
objs[1] = "1";
t.Start(objs);
Console.WriteLine("received message with contentId = " + request.contentId);
}
}
catch (Exception ex)
{
CURRENT_ACTIVE_THREAD -= 1;
Console.WriteLine("Exception: " + ex.Message);
DownloadController.WriteLog("There is exception while trying to read message from message queue, Exception = " + ex.Message);
}
}
}
}
}
那么,谁能告诉我问题是什么?为什么会这样?
【问题讨论】:
-
有什么例外吗?队列中有更多消息吗?弄清楚这些事情的最好方法是抽象出实际的队列。然后用内存中的集合替换队列并针对它测试您的代码。一旦你这样做了,看看你的代码是如何表现的,这将告诉你你的代码是否正常工作。你想知道为什么没有人回答你的问题吗?这是因为没有人拥有您的
MessageQueue,所以我们无法测试您的代码。 -
@CodingYoshi:消息队列总是有数据,因为有另一个应用程序作为网络服务器(owinSelftHost)向消息队列添加消息,我可以通过我的检查消息队列是否有数据自己的眼睛,如果消息队列中没有数据,为什么当我右键单击应用程序的控制台时,它会继续从消息队列中读取数据?
-
而且,没有例外。
标签: c# multithreading console-application msmq