mCore™ .NET SMS Library 1.2

mCore.SMS.ThreadLockTimeout Property

Description:

mCore properties and methods that require serial communication with the GSM Modem are run internally on a separate thread each. In addition to this, certain background  process like new incoming message events, delivery status reports, message queue etc are also run on separate thread each. In order to avoid a clash of shared resources between these threads (viz. serial port communication etc), managed threading is implemented using acquiring exclusive locks for shared resources within mCore.

The ThreadLockTimeout property defines the maximum time, in milliseconds, for which a thread will wait to acquire a exclusive lock before abandoning the process in that thread and throwing a exception. default value is set to 300000 (i.e. 5 minutes). Normally this value will not required to be modified, however, if your application requires calling of too many properties and methods of mCore from multiple threads then increasing this value may be required. Minimum value is 60000 (i.e. 1 minute)


Namespace: mCore
Assembly: mCoreLib (in mCoreLib.dll)

Exceptions:

mCore.GeneralException

Usage Example:

Visual Basic
Public Class MyClass
   Public WithEvents objSMS As New mCore.SMS
   Public Sub SendQueue()
      Dim strResult As String = ""
      Dim strMsg As String = "This is a test message"
      Dim strKey as String
      Try
         objSMS.ThreadLockTimeout = 600000
         SetCommParameters()
         objSMS.Encoding = mCore.Encoding.GSM_Default_7Bit
         objSMS.LongMessage = mCore.LongMessage.Concatenate
         objSMS.DeliveryReport = True

         strKey = objSMS.SendSMSToQueue("+919873094767", strMsg, _
                     mCore.QueuePriority.High)
         MsgBox("Message added to queue." & vbCrLf & _
                  "[Queue Key: " & strKey & "]")

         objSMS.DeliveryReport = False
         'No delivery report for the following message
         strKey = objSMS.SendSMSToQueue("+919810098765", strMsg)
         MsgBox("Message added to queue." & vbCrLf & _
                  "[Queue Key: " & strKey & "]")

         objSMS.DeliveryReport = True
         strKey = objSMS.SendSMSToQueue("+919811045678", strMsg, _
                     mCore.QueuePriority.Low)
         MsgBox("Message added to queue." & vbCrLf & _
                  "[Queue Key: " & strKey & "]")

         'Message queue is enabled
         objSMS.Queue.Enabled = True

         'Messages can be added to the queue even
         'when the Message queue is enabled

         strKey = objSMS.SendSMSToQueue("+919873012345", strMsg, _
                     mCore.QueuePriority.High)
         MsgBox("Message added to queue." & vbCrLf & _
                  "[Queue Key: " & strKey & "]")

      Catch ex As mCore.GeneralException
         MsgBox(ex.Message, MsgBoxStyle.Critical, "mCore Demo")
      Catch ex As Exception
      End Try
   End Sub

   'Event at the end of sending a queued message
   Private Sub objSMS_QueueSMSSent(ByVal sender As Object, _
               ByVal e As mCore.QueueSMSSentEventArgs) _
               Handles objSMS.QueueSMSSent
      If e.ErrorCode > 0 Then
         MsgBox("Message sending FAILED to: " & e.DestinationNumber & _
               vbCrLf & "[ERROR: " & e.ErrorDescription & _
               "]" & vbCrLf & "[Queue Key: " & e.QueueMessageKey & _
               "]", MsgBoxStyle.Critical, "MESSAGE FAILED")
      Else
         MsgBox("Message SENT to: " & e.DestinationNumber & vbCrLf & _
               "[Queue Key: " & e.QueueMessageKey & _
               "]", MsgBoxStyle.Information, "MESSAGE SENT")
      End If
   End Sub

   'Delivery Report Event Handling
   'A message box will pop-up whenever
   'a delivery report is received

   Private Sub objSMS_NewDeliveryReport(ByVal sender As Object, _
               ByVal e As mCore.NewDeliveryReportEventArgs) _
               Handles objSMS.NewDeliveryReport
      If e.Status Then
         MsgBox("MESSAGE DELIVERED" & vbCrLf & vbCrLf & "TO: " & _
               e.Phone & vbCrLf & vbCrLf & "DELIVERY DATE/TIME: " _
               & e.DeliveryTimeStamp.ToString & vbCrLf & vbCrLf & _
               "[Message Ref.: " & e.MessageReference.ToString & _
               "]", MsgBoxStyle.Information, "DELIVERY REPORT")
      Else
         MsgBox("MESSAGE DELIVERY FAILED" & vbCrLf & vbCrLf & _
               "TO: " & e.Phone & vbCrLf & vbCrLf & _
               "REPORT DATE/TIME: " & e.DeliveryTimeStamp & _
               vbCrLf & vbCrLf & "[Message Ref.: " & _
               e.MessageReference.ToString & "]", _
               MsgBoxStyle.Critical, "DELIVERY REPORT")
      End If
   End Sub

   Public Sub SetCommParameters()
      Try
         objSMS.Port = "COM1"
         objSMS.BaudRate = mCore.BaudRate.BaudRate_19200
         objSMS.DataBits = mCore.DataBits.Eight
         objSMS.StopBits = mCore.StopBits.One
         objSMS.Parity = mCore.Parity.None
         objSMS.FlowControl = mCore.FlowControl.RTS_CTS

      Catch ex As mCore.GeneralException
         MsgBox(ex.Message)
      Catch ex As Exception
      End Try
   End Function
End Class
 
C#

class MyClass
{
   public mCore.SMS objSMS = new mCore.SMS();

   public MyClass()

   {
      objSMS.NewDeliveryReport +=
         new mCore.SMS.NewDeliveryReportEventHandler(objSMS_NewDeliveryReport);

      objSMS.QueueSMSSent +=
         new mCore.SMS.QueueSMSSentEventHandler(objSMS_QueueSMSSent);
   }

   public static void SendQueue()
   {
      string strResult = "";
      string strMsg = "This is a test message";
      string strKey = "";
      try
      {
         objSMS.ThreadLockTimeout = 600000;
         SetCommParameters();
         objSMS.Encoding = mCore.Encoding.GSM_Default_7Bit;
         objSMS.LongMessage = mCore.LongMessage.Concatenate;
         objSMS.DeliveryReport = true;

         strKey = objSMS.SendSMSToQueue
                        ("+919873094767", strMsg, mCore.QueuePriority.High);
         MessageBox.Show("Message added to queue.\r\n" +
                  "[Queue Key: " + strKey + "]");

         objSMS.DeliveryReport = false;
         //No delivery report for the following message
         strKey = objSMS.SendSMSToQueue
                        ("+919810098765", strMsg, mCore.QueuePriority.Low);
         MessageBox.Show("Message added to queue.\r\n" +
                  "[Queue Key: " + strKey + "]");

         objSMS.DeliveryReport = true;
         strKey = objSMS.SendSMSToQueue
                        ("+919811045678", strMsg, mCore.QueuePriority.High);
         MessageBox.Show("Message added to queue.\r\n" +
                  "[Queue Key: " + strKey + "]");

         //Message queue is enabled
         objSMS.Queue.Enabled = true;

         //Messages can be added to the queue even
         //when the Message queue is enabled

         strKey = objSMS.SendSMSToQueue
                        ("+919873012345", strMsg, mCore.QueuePriority.High);
         MessageBox.Show("Message added to queue.\r\n" +
                  "[Queue Key: " + strKey + "]");
      }

      catch (mCore.GeneralException e)
      {
         MessageBox.Show(e.ToString());
      }
      catch (Exception e)
      {
      }
   }

   //Event at the end of sending a queued message
   private void objSMS_QueueSMSSent
      (object sender, mCore.QueueSMSSentEventArgs e)
   {
      if (e.ErrorCode > 0)
      {
         MessageBox.Show("Message sending FAILED to: " + e.DestinationNumber + _
               "/r/n" + "[ERROR: " + e.ErrorDescription + _
               "]/r/n[Queue Key: " & e.QueueMessageKey + "]");
      }
      else
      {
         MessageBox.Show("Message SENT to: " + e.DestinationNumber + _
               "/r/n" + "[ERROR: " + e.ErrorDescription + _
               "]/r/n[Queue Key: " & e.QueueMessageKey + "]");
      }
   }

   //Delivery Report Event Handling
   //A message box will pop-up whenever
   //a delivery report is received

   private void objSMS_NewDeliveryReport
      (object sender, mCore.NewDeliveryReportEventArgs e)
   {
      if (e.Status)
      {
         MessageBox.Show("MESSAGE DELIVERED" + "/r/n/r/n" +
            "TO: " + e.Phone + "/r/n/r/n" +
            "DELIVERY DATE/TIME: " + e.DeliveryTimeStamp.ToString() +
            "/r/n/r/n" + "[Message Ref.: " +
            e.MessageReference.ToString() + "]");
      }
      else
      {
         MessageBox.Show("MESSAGE DELIVERY FAILED" + "/r/n/r/n" +
            "TO: " + e.Phone + "/r/n/r/n" +
            "REPORT DATE/TIME: " + e.DeliveryTimeStamp.ToString() +
            "/r/n/r/n" + "[Message Ref.: " +
            e.MessageReference.ToString() + "]");
      }
   }

   public static void SetCommParameters()
   {
      try
      {
         objSMS.Port = "COM1";
         objSMS.BaudRate = mCore.BaudRate.BaudRate_19200;
         objSMS.DataBits = mCore.DataBits.Eight;
         objSMS.StopBits = mCore.StopBits.One;
         objSMS.Parity = mCore.Parity.None;
         objSMS.FlowControl = mCore.FlowControl.RTS_CTS;
      }

      catch (mCore.GeneralException e)
      {
         MessageBox.Show(e.ToString());
      }
      catch (Exception e)
      {
      }
   }
}

See Also:

 

 

Copyright © IG Logix Softech Pvt Ltd, All Rights Reserved