title: About Jamaa SMPP Asynchronous operation
body: <p>I have successfully implemented an application using Jamaa Lib and Jamaa Net. Thanks to the developers. </p>
  
  <p>But I am facing a problem when I use Asynchronous sending operation. An unhandled exception is being raised continuously. Would be highly appreciated if someone can help me to figure out the problem. </p>
  
  <p>Please be noted. I am receiving and sending SMS using the same application. I need to send on an average 100,000 SMS per day. Simultaneously I am receiving SMS from SMSC also. Thanks in advance. </p>
  
  <p>My Code snippet </p>
  
  <pre><code>namespace Transciever 
  {
  class Program
  {
  
      static void Main(string[] args)
      {
          SmppClient client = new SmppClient();
          MySQLConnect con = new MySQLConnect();
          TextMessage msg = new TextMessage();
          Settings settings = new Settings();
          SmppConnectionProperties properties = client.Properties;
          string sysid = ConfigurationManager.AppSettings["SystemID"];
          string pswd = ConfigurationManager.AppSettings["Password"];
          string hst = ConfigurationManager.AppSettings["Host"];
          int port = Int32.Parse(ConfigurationManager.AppSettings["Port"]);
          properties.SystemID = sysid;
          properties.Password = pswd;
          properties.Port = port ; //IP port to use
          properties.Host = hst; //SMSC host name or IP Address
          properties.SystemType = "";
          properties.DefaultServiceType = "";
          //Resume a lost connection after 30 seconds
          client.AutoReconnectDelay = 3000;
          //SmppClient client1=GetSmppClient();
          //Send Enquire Link PDU every 15 seconds
          client.KeepAliveInterval = 15000;
  
          //Start smpp client
          try
          {
              client.Start();
  
          }
          catch (Exception ex)
          {
  
  
          }
          client.MessageReceived += new EventHandler&lt;MessageEventArgs&gt;(client_MessageReceived);
          while (true)
          {
              client.ConnectionStateChanged += (sender, e) =&gt;
              {
  
                  switch (e.CurrentState)
                  {
  
                      case SmppConnectionState.Closed:
                          // Connection to the remove server is lost
  
                          try
                          {
                              client.Start();
  
                          }
                          catch (Exception ex)
                          {
  
                          }
  
                          break;
  
                      case SmppConnectionState.Connected:
                          //Console.Write("Connected");
                          break;
  
                      case SmppConnectionState.Connecting:
                          break;
  
                  }
  
              };
  
  
              String successQuery = "";
              var list = new List&lt;smsoutbox&gt;();
              list = con.Select("select msgID, srcMN,dstMN, msg from table where msgStatus='QUE' order by msgID desc LIMIT 500");
              int listcount = list.Count;
              int i = 0;
              if (listcount &gt; 0 &amp;&amp; client.ConnectionState == SmppConnectionState.Connected)
              {
                  foreach (var row in list)
                  {
                      i = i + 1;
                      msg.DestinationAddress = row.dstMN;
                      msg.Text = row.msg;
                      msg.SourceAddress = row.srcMN;
                      msg.RegisterDeliveryNotification = false;
                      successQuery += row.msgID.ToString() + ",";
                      try
                      {
                          client.BeginSendMessage(msg, SendMessageCompleteCallback, client);                        
  
                      }
                      catch (Exception ex)
                      {
  
  
                      }
                      if (i == 250)
                      {
                          try
                          {
                              if (successQuery.Length &gt; 0)
                              {
                                  successQuery = successQuery.Substring(0, successQuery.Length - 1);
                                  successQuery = "update smsoutbox_bl set msgStatus='SENT' where msgID in (" + successQuery + ")";
                                  con.Update(successQuery);
                                  successQuery = "";
                              }
                          }
                          catch (Exception ex)
                          {
                          }
                          i = 0;
                      }//if i==250 ends
  
                  }//foreach ends
              }//if listcount&gt;0 ends
  
              if (i &gt; 0)
              {
                  try
                  {
                      if (successQuery.Length &gt; 0)
                      {
                          successQuery = successQuery.Substring(0, successQuery.Length - 1);
  
                          successQuery = "update smsoutbox_bl set msgStatus='SENT' where msgID in (" + successQuery + ")";
                          //dbConnection.RunSql(successQuery);
                          con.Update(successQuery);
                          successQuery = "";
                      }
                  }
                  catch (Exception ex)
                  {
                  }
              }
          }//while loop ends
      }//main ends
  
      static public void client_MessageReceived(object sender, MessageEventArgs e)
      {
          //The event argument e contains more information about the received message
          TextMessage textMsg = e.ShortMessage as TextMessage; //This is the received text message
          try
          {
              MyWebRequest myRequest = new MyWebRequest("http://localhost/cde/cdeblrecv.php", "POST", "mn=" + textMsg.SourceAddress + "&amp;msg=" + textMsg.Text + "&amp;sc=3333");
  
          }
          catch(Exception ex)
          {
          }
      }
  
      static private void SendMessageCompleteCallback(IAsyncResult result)
      {
          SmppClient client = (SmppClient)result.AsyncState;
          client.EndSendMessage(result);
      }
  
  
  }//class program ends here
  }//namespace ends here 
  </code></pre>
  
  <hr>
  
  <p>Below is the exception thrown by the app. </p>
  
  <blockquote>
    <p>Unhandled Exception: JamaaTech.Smpp.Net.Lib.SmppException: Exception
    of type 'JamaaTech.Smpp.Net.Lib.SmppException' was thrown.</p>
  </blockquote>
  
  <p>Server stack trace: </p>
  
  <blockquote>
    <p>at JamaaTech.Smpp.Net.Client.SmppClient.SendMessage(ShortMessage
    message, Int32 timeOut)  at
    System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr
    md, Object[] args, Object server, Object[]&amp; outArgs)  at
    System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage
    msg, IMessageSink replySink) </p>
    
    <p>Exception rethrown at [0]:  at
    System.Runtime.Remoting.Proxies.RealProxy.EndInvokeHelper(Message
    reqMsg, Boolean bProxyCase)  at
    System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(Object NotUsed,
    MessageData&amp; msgData)  at
    JamaaTech.Smpp.Net.Client.SendMessageCallBack.EndInvoke(IAsyncResult
    result)  at
    JamaaTech.Smpp.Net.Client.SmppClient.EndSendMessage(IAsyncResult
    result)  at
    Transciever.Program.SendMessageCompleteCallback(IAsyncResult result)
    in
    d:\smpp\transciever\appTranscieverinCSharp\appTranscieverinCSharp\Program.cs:line
    223  at
    System.Runtime.Remoting.Messaging.AsyncResult.SyncProcessMessage(IMessage
    msg)  at
    System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage
    msg, IMessageSink replySink)  at
    System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(Object
    o)  at
    System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object
    state)  at
    System.Threading.ExecutionContext.RunInternal(ExecutionContext
    executionContext, ContextCallback callback, Object state, Boolean
    preserveSyncCtx)  at
    System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback callback, Object state, Boolean
    preserveSyncCtx)  at
    System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
    at System.Threading.ThreadPoolWorkQueue.Dispatch()  at
    System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</p>
  </blockquote>