Receiving SMS messages with multiple threads

Aug 10, 2012 at 3:08 PM

Hello

I have been using the lib for receiving SMS only. I handle the received text with the event handler as suggested in the How To.

But on the "Multi-Threading Issues" How to it says that the programmer must " ensure that all SmppClient event handlers, and other methods accessible from the event handlers are thread-safe"

Does this mean that in the event-handler one has to add some sort of lock when reading the value of the"MessageEventArgs e", something like this:

        public static void client_MessageReceived(object sender, MessageEventArgs e)
        {

       lock(my_object)

          {

           //The event argument e contains more information about the received message
            TextMessage textMsg = e.ShortMessage as TextMessage; //This is the received text message
            Console.WriteLine("AddressTon: {0}, SourceAddress: {1}, AddressNpi: {2}", c.Properties.AddressTon.ToString(), c.Properties.SourceAddress, c.Properties.AddressNpi.ToString());
            string msgInfo = string.Format("Sender: {0}, Receiver: {1}, Message content: {2}", textMsg.SourceAddress, textMsg.DestinationAddress, textMsg.Text);
            //Display message
            Console.WriteLine(msgInfo);

         }

         }

can somebody post an example on how to make this receiving process thread safe?

thank you, great library btw


 


Coordinator
Aug 17, 2012 at 6:02 PM
The "MessageReceived" event handler is called by a random thread from the thread pool and it is possible that it can be called simultaneously by more than one thread.

The code snipped you provided is thread safe, and thus should be able to do it.
Aug 17, 2012 at 6:21 PM

Thank you,