简体   繁体   English

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

[英]C# Console application pause working until right click on Console

I'm building ac# console application which read messages from MSMQ(Microsoft Message Queuing), I expected it to run forever but after a day running it stop reading message from MSMQ until I right click on its Console, my problem seems to be alittle bit similar to this: " Console Application "pausing" c# ". 我正在构建一个从MSMQ(Microsoft消息队列)读取消息的ac#控制台应用程序,我希望它可以永远运行,但是一天运行后,它停止从MSMQ读取消息,直到我右键单击其控制台为止,我的问题似乎很难解决与此类似:“ 控制台应用程序”暂停“ c# ”。 Bellow is the function that I'm using: 贝娄是我正在使用的功能:

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

                    }
                }
            }
        }
    }

So,could anyone please tell me what the problem is? 那么,有人可以告诉我问题出在哪里吗? Why this happening? 为什么会这样呢?

It might be you're while loop. 可能是您在while循环中。 I had while loops freeze or break my applications before. 我之前的while循环冻结或破坏了我的应用程序。 Might i suggest using timers instead ? 我可能建议改用计时器吗? I have some links you could use for reference: 我有一些链接可供您参考:

c# wait for a while without blocking C#等待一段时间而不阻塞

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

I do hope this fixes the problem you're having ! 我希望这能解决您遇到的问题!

greatings, Ramon. 贺礼,拉蒙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM