C#控制台应用程序暂停工作,直到右键单击Console

时间:2018-04-19 02:19:04

标签: c# multithreading console-application msmq

我正在构建从MSMQ(Microsoft消息队列)读取消息的ac#console应用程序,我希望它能够永久运行但是在运行一天后它停止从MSMQ读取消息,直到我右键单击其控制台,我的问题似乎与此类似:" Console Application "pausing" c#"。贝娄是我正在使用的功能:

     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);

                    }
                }
            }
        }
    }

那么,有谁能告诉我这是什么问题?为什么会这样?

1 个答案:

答案 0 :(得分:0)

可能是你在循环中。我之前有循环冻结或破坏我的应用程序。我建议使用计时器吗?我有一些你可以用来参考的链接:

c# wait for a while without blocking

https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

我希望这可以解决您遇到的问题!

greatings, 拉蒙。