private void Run()
    {
        Message msg = null;

        Console.WriteLine("Subscribing to destination: " + name + "\n");

        ConnectionFactory factory = new TIBCO.EMS.ConnectionFactory(serverUrl);

        // create the connection
        connection = factory.CreateConnection(userName, password);

        // create the session
        session = connection.CreateSession(false, ackMode);

        // set the exception listener
        connection.ExceptionListener = this;

        // create the destination
        if (useTopic)
        {
            destination = session.CreateTopic(name);
        }
        else
        {
            destination = session.CreateQueue(name);
        }

        // create the consumer
        msgConsumer = session.CreateConsumer(destination);

        // start the connection
        connection.Start();

        // read messages
        while (true)
        {
            // receive the message
            msg = msgConsumer.Receive();
            if (msg == null)
            {
                break;
            }

            if (ackMode == Session.CLIENT_ACKNOWLEDGE ||
                ackMode == Session.EXPLICIT_CLIENT_ACKNOWLEDGE ||
                ackMode == Session.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE)
            {
                msg.Acknowledge();
            }

            Console.WriteLine("Received message: " + msg);
            if (msg is BytesMessage)
            {
                BytesMessage bm = (BytesMessage)msg;
                Console.WriteLine(bm.ReadBoolean());
                Console.WriteLine(bm.ReadChar());
                Console.WriteLine(bm.ReadShort());
                Console.WriteLine(bm.ReadInt());
                Console.WriteLine(bm.ReadLong());
                Console.WriteLine(bm.ReadFloat());
                Console.WriteLine(bm.ReadDouble());
            }
        }

        // close the connection
        connection.Close();
    }
        public void Consumer()
        {
            int ackMode = Session.AUTO_ACKNOWLEDGE;

            TIBCO.EMS.Message msg = null;

            while (true)
            {
                try
                {
                    // receive the message
                    msg = msgConsumer.Receive();
                }
                catch (EMSException EMSex)
                {
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + EMSex.Message; });
                    logger.Error(EMSex.Message);
                }
                if (msg == null)
                {
                    break;
                }

                if (ackMode == Session.CLIENT_ACKNOWLEDGE ||
                    ackMode == Session.EXPLICIT_CLIENT_ACKNOWLEDGE ||
                    ackMode == Session.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE)
                {
                    msg.Acknowledge();
                }

                LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\nReceived message: " + msg; });
                logger.Info("Received message: " + msg);

                if (msg is TextMessage)
                {
                    TextMessage tm = (TextMessage)msg;
                    //ReceveMsgTextBox.Text = tm.Text;
                    ReceveMsgTextBox.Invoke((MethodInvoker) delegate() { ReceveMsgTextBox.Text = tm.Text; });
                    logger.Info(tm.Text);
                }
                if (msg is BytesMessage)
                {
                    BytesMessage bm = (BytesMessage)msg;
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadBoolean(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadChar(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadShort(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadInt(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadLong(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadFloat(); });
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + bm.ReadDouble(); });
                }

                if (msg is MapMessage)
                {
                    MapMessage mm       = (MapMessage)msg;
                    var        mapNames = mm.GetMapNames();
                    LogTextBox.Invoke((MethodInvoker) delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.ToString(); });

                    /*   foreach (String name in mapNames)
                     * {
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetBoolean(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetByte(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetBytes(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetChar(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetShort(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetInt(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetLong(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetFloat(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetDouble(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetString(name); });
                     *     LogTextBox.Invoke((MethodInvoker)delegate() { LogTextBox.Text = LogTextBox.Text + "\n" + mm.GetObject(name); });
                     * }*/
                }
            }
        }