I am working on a project that involves reading messages from a sim, inserting them into a MySQL database table and replying to each of those messages. My sim can handle a maximum of 30 messages. Previously what i was doing was reading all the 30 messages at a time, inserting them into the database and at the same time deleting them from the sim so that new messages can come in. After the messages are in table I take one message at a time and reply to it and at the same time delete it from the table.
Recently I tried a new approach using threads. Now, the messages are read in to table and replied to at the same time. This has greatly increased performance and decreased the reply time but problems have occurred. In the following code, I sometimes get an exception on the connection.open()
function after it's been able to successfully connect for 30-60 seconds. The exception is like "error connecting to MySQL server using localhost".
The program works fine on my PC, but the failure occurs on other computers.
private void btnReadMessages_Click(object sender, System.EventArgs e)
{
Thread.CurrentThread.Name = "Main";
Thread ReadMsgsThread = new Thread(new ThreadStart(delegate
{
while (true)
{
readMsgs();
}
}));
Thread ReplyMsgsThread = new Thread(new ThreadStart(delegate
{
while (true)
{
replyMsgs();
}
}));
ReadMsgsThread.Start();
Output("Reading Messages..");
Output("");
ReplyMsgsThread.Start();
}
private void readMsgs()
{
String connectionString = "DRIVER={MySQL ODBC 3.51Driver};Database=sms;Server=localhost;UID=hasnat;PWD=123;";
OdbcConnection connection = new OdbcConnection(connectionString);
Cursor.Current = Cursors.WaitCursor;
string storage = GetMessageStorage();
connection.Open();
try
{
DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.All, storage);
if (messages.Length != 0)
{
foreach (DecodedShortMessage message in messages)
{
SmsDeliverPdu data = (SmsDeliverPdu)message.Data;
OdbcCommand command = new OdbcCommand("INSERT INTO smstable (sms,phone) VALUES ('" + data.UserDataText + "','" + data.OriginatingAddress + "')", connection);
command.ExecuteNonQuery();
delMsgAtIndex(message.Index);
}
}
}
catch (Exception ex)
{
ShowException(ex);
}
connection.Close();
Thread.Sleep(5000);
Cursor.Current = Cursors.Default;
}
private void replyMsgs()
{
String connectionString = "DRIVER={MySQL ODBC 3.51 Driver};Database=sms;Server=localhost;UID=hasnat;PWD=123;";
OdbcConnection connection = new OdbcConnection(connectionString);
connection.Open();
try
{
OdbcCommand commandSelect = new OdbcCommand("SELECT id,phone,sms FROM smstable", connection);
OdbcDataReader dr = commandSelect.ExecuteReader();
while (dr.Read())
{
System.Threading.Thread.Sleep(5000);
if (dr["sms"].ToString().Length == 13)
{
OdbcCommand commandGetInfo = new OdbcCommand("SELECT nic,name,hospital,accType,date,status FROM patients where nic='"+dr["sms"].ToString()+"';", connection);
OdbcDataReader getInfo = commandGetInfo.ExecuteReader();
if (getInfo.HasRows)
{
while (getInfo.Read())
{
sendMsg(dr["phone"].ToString(), "Name: "+getInfo["name"].ToString() + " Hospital: " + getInfo["hospital"].ToString() + " Accident: " + getInfo["accType"].ToString() + " Date: " + getInfo["date"].ToString() + " Status: " + getInfo["status"].ToString());
OdbcCommand commandDelRepliedSMS = new OdbcCommand("Delete FROM smstable where id=" + dr["id"].ToString(), connection);
OdbcDataReader drDelRepliedSMS = commandDelRepliedSMS.ExecuteReader();
}
}
else
{
sendMsg(dr["phone"].ToString(), "No Patient Record Found For Provided NIC,Check NIC or Visit Our Website www.emg1122.com For More Detailed Search");
OdbcCommand commandDelNotFoundSMS = new OdbcCommand("Delete FROM smstable where id=" + dr["id"].ToString(), connection);
OdbcDataReader drDelNotFoundSMS = commandDelNotFoundSMS.ExecuteReader();
}
}
else
{
sendMsg(dr["phone"].ToString(),"NIC Should Be 13 Digits Long And No Dashes or Alphabets");
OdbcCommand commandDelWrongSMS = new OdbcCommand("Delete FROM smstable where id=" + dr["id"].ToString(), connection);
OdbcDataReader drDelWrongSMS = commandDelWrongSMS.ExecuteReader();
}
}
}
catch (Exception ex)
{
ShowException(ex);
}
connection.Close();
}
private void sendMsg(String phno,String reply)
{
Cursor.Current = Cursors.WaitCursor;
try
{
// Send an SMS message
Output("Replying To:{0}", phno);
SmsSubmitPdu pdu;
pdu = new SmsSubmitPdu(reply, phno);
comm.SendMessage(pdu);
Output("Message Replied To {0}", phno);
Output("");
}
catch (Exception ex)
{
ShowException(ex);
}
Cursor.Current = Cursors.Default;
}
private void delMsgAtIndex(int index)
{
Cursor.Current = Cursors.WaitCursor;
string storage = GetMessageStorage();
try
{
// Delete the message with the specified index from storage
comm.DeleteMessage(index, storage);
}
catch (Exception ex)
{
ShowException(ex);
}
Cursor.Current = Cursors.Default;
}