发送许多短信时,批量短信应用程序挂起

时间:2016-02-17 02:38:16

标签: c# winforms oracle11g devexpress gsmcomm

我使用 GsmComm 库, Oracle 数据库和 Devexpress 为批量短信创建了简单的应用程序。我发送了许多消息(超过25条消息)我的应用程序挂起的消息。我认为这是因为过程太多了。方案,当用户发送消息时它将存储到数据库( OUTBOX 表),然后用户点击发件箱表单上的按钮,如果消息成功发送它将存储到数据库( SENTMESSAGE table)并删除在 OUTBOX 表中发送成功的消息。当消息处理发送时用户无法点击另一个按钮或菜单,但在完成发送后,应用程序再次恢复正常,用户可以点击许多菜单。我阅读了一些文章,如果使用 Gammu ,它可能是因为它是第三方应用程序,可以作为发送和接收消息的服务运行,但我需要在 gsmcomm 库中。< / p>

详细信息,这是我的代码:

//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");
  }
}

我希望用户在用户发送邮件时能够点击我的应用程序中的其他菜单。 我不知道如何解决这个问题,也许任何人都可以给我一个建议和任何概念,我将非常感激。 感谢..

2 个答案:

答案 0 :(得分:3)

嗨尝试在这里使用线程

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");
      }
    }

答案 1 :(得分:1)

我同意Tanmay,使用线程,但我也认为如果可能使用单个表,即不要使用不同的表用于发件箱或发送消息,只需使用标志来保持状态,这将减少DB I / 0