0

I'm building a c# 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#". 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?

user2905416
  • 404
  • 7
  • 21
  • Any exceptions? Do you have more messages in queue? The best way to figure such things is to abstract out the actual queue. Then replace the queue with an in-memory collection and test your code against it. Once you do that see how your code behaves and that will tell you if your code is working. Are you wondering why no one is answering your question? It's because no one has your `MessageQueue` so we cannot test your code. – CodingYoshi Apr 19 '18 at 03:17
  • @ CodingYoshi:The message queue always has data because there is another application work as a webserver(owinSelftHost) add messages to the messages queue and I can check if whether the message queue has data or not by my own eyes, if the message queue doesn't has data in it why when I right click on the application's console it continues reading data from message queue? – user2905416 Apr 19 '18 at 04:32
  • And, there is no Exception. – user2905416 Apr 19 '18 at 04:39

1 Answers1

0

It might be you're while loop. I had while loops freeze or break my applications before. Might i suggest using timers instead ? I have some links you could use for reference:

c# wait for a while without blocking

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

I do hope this fixes the problem you're having !

greatings, Ramon.

Ramon
  • 1
  • 1