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

本文关键字:许多短 挂起 应用程序 | 更新日期: 2023-09-27 18:35:00

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

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

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

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

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

嗨,

尝试在这里使用线程

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,使用线程,但我也认为如果可能的话使用单个表,即不要对发件箱或发送的消息使用不同的表,只需使用标志来保持状态,这将减少数据库 I/O