1

i have created simple application to bulk sms using GsmComm library , Oracle database and Devexpress. I sent message for many messages (more than 25 messages) my application got hang. i assume it because of too many process. the scenario, when a user sent message it will store to database (OUTBOX table), then user click button on outbox form, if message success to sent it will store to database (SENTMESSAGE table) and delete message where sent success in the OUTBOX table. when the message is processing to sent user couldn't click for another button or menu, but after it finished to sent ,application got normal again and user could click for many menus. i read some articles that it possible if used Gammu because it is third party application and can run as service to send and receive message, but i need it in gsmcomm library.

for detail, this my codes:

//SEND BULK SMS MORE THAN 30 MESSAGE APPLICATION HANG
if (CommSetting.comm.IsConnected() == true)
{
    int i;
    for (i = 0; i < gridView1.DataRowCount; i++)
    {
        string pesan = gridView1.GetRowCellValue(i,"MESSAGE").ToString();
        string ttd = lblTandaTangan.Text;
        string msg = pesan + Environment.NewLine + Environment.NewLine + ttd;  //SENT MESSAGE USING SIGNATURE

    var listPhoneNumber = new List<string>();
    listPhoneNumber.Add(gridView1.GetRowCellValue(i, "PHONENUMBER").ToString());

    foreach (var phoneNumber in listPhoneNumber)
    {
        var pdu = new SmsSubmitPdu(msg, phoneNumber, string.Empty);
        CommSetting.comm.SendMessage(pdu);

        //---------------STORE TO ORACLE DB--------------------
        if (koneksi_manual.con.State == ConnectionState.Open)
        {
            koneksi_manual.con.Close();
        }
        koneksi_manual.con.Open();

        OracleCommand cmd = new OracleCommand();
        cmd.CommandText = @"INSERT INTO MESSAGESENT (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES 
                        (SQ_MESSAGESENT.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
                            + gridView1.GetRowCellValue(i, "PHONENUMBER") + "', '" + pesan.Replace("'", "''") + "', '" + Program.IDUser + "')";// <= Use gridView1.GetRowCellValue to get the cell value.
        cmd.Connection = koneksi_manual.con;
        cmd.ExecuteNonQuery();

        //Sleeps system for 1000ms for refreshing GSM Modem
        System.Threading.Thread.Sleep(1000);

        //DELETE MESSAGE SENT FROM OUTBOX
        var obj = gridView1.GetRowCellValue(i, "ID");

        OracleCommand cmd2 = new OracleCommand();
        cmd2.CommandText = "DELETE FROM OUTBOX WHERE ID = '" + obj + "'";
        cmd2.Connection = koneksi_manual.con;
        cmd2.ExecuteNonQuery();
    }
    MessageBox.Show("Message Sent", "Notif");
  }
}

I want users to be able click for another menu in my application when user is sending messages. i don't know how to solve this, perhaps anybody can giving me a suggest and any concept, i will really appreciate this. Thanks..

aminvincent
  • 553
  • 1
  • 12
  • 43

2 Answers2

3

hi try to use threading over here

Control.CheckForIllegalCrossThreadCalls = false;
 Private Void BtnSend_Click(Object Sender,EventArg e)
    {
      Thread th = new Thread(new ThreadStart(SendMSM));
    }



 Private void SendMSM()
    {
    if (CommSetting.comm.IsConnected() == true)
    {
        int i;
        for (i = 0; i < gridView1.DataRowCount; i++)
        {
            string pesan = gridView1.GetRowCellValue(i,"MESSAGE").ToString();
            string ttd = lblTandaTangan.Text;
            string msg = pesan + Environment.NewLine + Environment.NewLine + ttd;  //SENT MESSAGE USING SIGNATURE

        var listPhoneNumber = new List<string>();
        listPhoneNumber.Add(gridView1.GetRowCellValue(i, "PHONENUMBER").ToString());

        foreach (var phoneNumber in listPhoneNumber)
        {
            var pdu = new SmsSubmitPdu(msg, phoneNumber, string.Empty);
            CommSetting.comm.SendMessage(pdu);

            //---------------STORE TO ORACLE DB--------------------
            if (koneksi_manual.con.State == ConnectionState.Open)
            {
                koneksi_manual.con.Close();
            }
            koneksi_manual.con.Open();

            OracleCommand cmd = new OracleCommand();
            cmd.CommandText = @"INSERT INTO MESSAGESENT (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES 
                            (SQ_MESSAGESENT.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
                                + gridView1.GetRowCellValue(i, "PHONENUMBER") + "', '" + pesan.Replace("'", "''") + "', '" + Program.IDUser + "')";// <= Use gridView1.GetRowCellValue to get the cell value.
            cmd.Connection = koneksi_manual.con;
            cmd.ExecuteNonQuery();

            //Sleeps system for 1000ms for refreshing GSM Modem
            System.Threading.Thread.Sleep(1000);

            //DELETE MESSAGE SENT FROM OUTBOX
            var obj = gridView1.GetRowCellValue(i, "ID");

            OracleCommand cmd2 = new OracleCommand();
            cmd2.CommandText = "DELETE FROM OUTBOX WHERE ID = '" + obj + "'";
            cmd2.Connection = koneksi_manual.con;
            cmd2.ExecuteNonQuery();
        }
        MessageBox.Show("Message Sent", "Notif");
      }
    }
Tanmay Nehete
  • 2,138
  • 4
  • 31
  • 42
  • i add `Control.CheckForIllegalCrossThreadCalls = false;` but my VS detect error like this `'System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls' is a property but is used like a type` how to solve this? – aminvincent Feb 17 '16 at 05:54
  • Add this in Form Constructor this not necessary, bt if it gives you cross threading problem then this will be useful – Tanmay Nehete Feb 17 '16 at 06:00
  • 1
    there is a instance variable `th` in btnSend `Thread th = new Thread(new ThreadStart(SendMSM));` what should i do with that variable? – aminvincent Feb 17 '16 at 06:56
  • 1
    thanks for your suggest but finally i used backgrounworker in vs and solve my problem,.. – aminvincent Feb 17 '16 at 09:41
1

I agree with Tanmay, to use threads, but I also think that if possible use single table, i.e. don't use different table for outbox or sent messages, just use a flag to keep status, this will reduce DB I/O