Пример #1
0
        //
        // FUNCTION : listen
        //
        // DESCRIPTION : This method will loop looking for messages sent from the server into the message queue. When found, it will
        //               format the message, close the queue and print the message to the screen.
        //
        // PARAMETERS : NA
        // RETURNS : None
        //
        private void listen()
        {
            while (!this.finished)//Listen forever or until the finished variable is set to true
            {
                try
                {
                    if ((clientQueue != null) && (myClient.connected = true))
                    {
                        message = new ClientInfo();

                        //timespan set for graceful shutdown
                        message = (ClientInfo)clientQueue.Receive(new TimeSpan(0, 0, 1)).Body;

                        string messRecv = message.message;

                        clientQueue.Close();

                        if (message != null)
                        {
                            this.Dispatcher.Invoke(() =>
                            {
                                //append received message to screen and scroll to most recent message
                                TextScreen.AppendText(messRecv + "\n");
                                TextScreen.ScrollToEnd();
                            });
                        }
                    }
                }
                catch (MessageQueueException mqex)
                {
                    //catching timeout exceptions and continuing loop (needed for graceful shutdown)
                    if (mqex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                    {
                        continue;
                    }

                    System.Windows.MessageBox.Show("MQ Exception: " + mqex.Message);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("Exception: " + ex.Message);
                }
            }
        }