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



我正在构建一个从 MSMQ(Microsoft 消息队列读取消息的 c# 控制台应用程序(,我希望它永远运行,但在运行一天后它停止从 MSMQ 读取消息,直到我右键单击其控制台,我的问题似乎有点类似于:"控制台应用程序"暂停"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);
                    }
                }
            }
        }
    }

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

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

C# 等待一段时间而不会阻塞

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

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

大金斯,雷蒙。

最新更新