Link to home
Start Free TrialLog in
Avatar of dpapkin
dpapkin

asked on

How do i reduce the 'lag' on my application?

Hi,

atm, im writing an application using VB. What it does is actually, reading sms frm the GSM modem into my databse, and it will send the sms out again at the time which is stated in the sms ealier on.

so now i have encounted a problem whereby some of the SMS, which is saved into my databse, are unable to send out zome of the SMSes.
EG.
1:55:00 PM          a
1:55:01 PM          b
1:55:02 PM          c
1:55:03 PM          d
1:55:04 PM          e
1:55:05 PM          f
1:55:06 PM          g
1:55:07 PM          h
1:55:08 PM          i
1:55:09 PM          j
1:55:10 PM          k
1:55:11 PM          l
1:55:12 PM          m
1:55:13 PM          n
1:55:14 PM          q

only a, f, j, n are recieved on my phone. the rest of it wld be skipped.
sometimes, of say if i were to run this program for 3 hrs.. in between minutes like. 3:02 PM, 3:03 PM, 3:04 PM.

only 3:02 and 3:04 PM would be sent.

my timer1 is 1000.

so is there anyway to eliminate such problems?
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

It depends on the way you are sending them. Maybe you cannot reduce the time needed to send an SMS and when you try to send one during a previous one is already processed, your operation simply failed and you didn't treated the error. Probably you decoupled the operation to send an SMS by using another thread. You should have a queue with all the message that are to be sent and them a thread which is reading sequencially the queue before sending them. And when you call "send" your thread should block until the end before dequeuing the next message.
Avatar of Shanmuga Sundaram D
Since you mentioned that your timer interval is set to 1000 then it is correct and your program should trigger sending sms for every second. But from your comment it is known that due to some reasons the timer is not able to execute every 1 second. So better try reducing the timer interval to less than 1000 and check whether it helps.
Avatar of dpapkin
dpapkin

ASKER

hmm ok i have tried putting the timer @ 500...

though the program is running, but the application window is not responsive !
ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can we see your looping logic and timer code?
Avatar of dpapkin

ASKER

i hope this part of the code helps.
private void timer1_Tick(object sender, EventArgs e)
        {
            
            string storage = GetMessageStorage();
            label1.Text = DateTime.Today.DayOfWeek.ToString();
            label2.Text = DateTime.Now.ToString();
            SmsSubmitPdu pdu;

            mycon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\My Documents\My Dropbox\FYP docs\website program\database\trialdata.mdb;Persist Security Info=False");

            OleDbCommand obj1 = new OleDbCommand("SELECT Student_Phone_Number, Text_Message , Reporting_Time ,Reporting_Day FROM studentremainder ", mycon);
            mycon.Open();

            OleDbDataReader aReader = obj1.ExecuteReader();

            string realtime = DateTime.Now.ToString();
            char[] delimiters = new char[] { '*' };
            string numbe;
            string number;
            string number0;
            string number1;
            string number2;
            OleDbCommand myCommand = new OleDbCommand();
           myCommand.Connection = mycon;
           // myCommand.CommandText = "INSERT INTO Query1( [Student_ID_adm], [Text_Message], [Reporting_Day], [Reporting_Time]) VALUES (@Student_Phone_number, @Student ID adm, @Text_Message, @Reporting_Day, @Reporting_Time)";
            //myCommand.CommandText = "INSERT INTO Query1([Text_Message], [Reporting_Day], [Reporting_Time])VALUES (@Text_Message, @day, @time)";
           myCommand.CommandText = "INSERT INTO Query1([Student_Phone_number],[Student_ID_adm],[Reporting_Day],[Reporting_Time],[Text_Message]) VALUES (ha,he,hou,hi,ju)";
           
            try
            {
                // Read all SMS messages from the storage
                
                storage = PhoneStorageType.Phone;
                DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.ReceivedUnread, storage);
                foreach (DecodedShortMessage message in messages)
                {
                    Output(string.Format("Message status = {0}, Location = {1}/{2}",
                        StatusToString(message.Status), message.Storage, message.Index));
                    ShowMessage(message.Data);
                    string h = message.Data.UserDataText.ToString();
                    Output(h);
                    Output("");
                    //
                    /*
                     if (h == "help")
                     {
                       pdu = new SmsSubmitPdu(message.data.orginatingaddress,"the format is....", "");
                     }
                     */
                    if ((h == "help") || (h == "Help"))
                    {
                        pdu = new SmsSubmitPdu("the format is <PhoneNo>*<AdminID>*<DD/MM/YYYY>*<H:MM:SS AM/PM>*<YourTextMessage>", num, "");
                        comm.SendMessage(pdu);
                    }

                    string[] parts = h.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    
                    for (int i = 0; i < parts.Length; i++)
                    {
                        //Output(parts[i]);
                        if (i == 0)
                        {
                            numbe = parts[0];
                         //   Output(numbe);

                            myCommand.Parameters.Add("ha", OleDbType.VarChar).Value = numbe.ToString();
                        }
                        if (i == 1)
                        {
                            number=parts[1];
                          //  Output(number);
                            
                           myCommand.Parameters.Add("he", OleDbType.VarChar).Value = number.ToString();
                           // myCommand.ExecuteNonQuery();
                        }
                        if (i == 2)
                        {
                            number0 = parts[2];
                        //    Output(number0);
                           // myCommand.CommandText = "INSERT INTO Query1([Text_Message]) VALUES (hou)";
                            myCommand.Parameters.Add("hou", OleDbType.VarWChar,10).Value = number0.ToString();
                           // myCommand.ExecuteNonQuery();
                        }
                        if (i == 3)
                        {
                            number1 = parts[3];
                          //  Output(number1);
                         //   myCommand.CommandText = "INSERT INTO Query1([Reporting_day]) VALUES (hi)";
                         myCommand.Parameters.Add("hi", OleDbType.VarWChar).Value = number1.ToString();
                        // myCommand.ExecuteNonQuery();
                        }
                        if (i == 4)
                        {
                            number2 = parts[4];
                        //    Output(number2);
                          myCommand.Parameters.Add("ju", OleDbType.VarWChar).Value = number2.ToString();
                          //myCommand.ExecuteNonQuery();
                          pdu = new SmsSubmitPdu("You have successfully registered ! ", num, "");
                          comm.SendMessage(pdu);
                        }
                   
                    }
                    
                    //myCommand.CommandText = "INSERT INTO Query1([Text_Message],[Reporting_Time]) VALUES ('number','number1')";// only allow string number and number2
                    myCommand.ExecuteNonQuery();
                }
               // Output(string.Format("{0,9} messages read.", messages.Length.ToString()));
                //Output("");
                
                while (aReader.Read())
                {
                    // Console.Write(aReader.GetString(2).ToString() + " ,");
                    // Console.Write(aReader.GetString(3).ToString() + " ,");
                    string c = aReader["Reporting_Time"].ToString();
                    string d = aReader["Reporting_Day"].ToString();
                    //label19.Text = c;
                    //label21.Text = d;
                    string g = d + " " + c;
                    //label5.Text = g;
                    if (g == realtime)
                    {
                        Console.Write(aReader.GetInt32(0).ToString() + " ,");//string(0) refer to phone
                        Console.Write(aReader.GetString(1).ToString() + " ,");//string(1) refer to message

                        string a = aReader["Text_Message"].ToString();
                        string b = aReader["Student_Phone_number"].ToString();
                        pdu = new SmsSubmitPdu(a, b, ""); // use for senting message
                        comm.SendMessage(pdu);
                    }
                }
                aReader.Close();
                mycon.Close();
            }
            catch (Exception ex)
            {
                ShowException(ex);
            }
            finally
            {

            }

            //  Cursor.Current = Cursors.Default;
        }

Open in new window

Try turning off the Timer, executing your code, then turning the Timer back on:
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            
            // ...all of your existing code here...

            timer1.Start();
        }

Open in new window

Avatar of dpapkin

ASKER

i dont quite understand you idle mind..

are u suggesting tat i...

1. add the timer1.start/stop into the codes
2. build the solution
3. delete away timer1.start/stop
4. build the solution again
5. run my program

yes?
No sir...you LEAVE the Start/Stop in the code.

When the Timer1 fires, you stop it via code in the Tick() event, execute your SMS code, then turn it back on at the end of the event.  This should prevent it from "stacking" on top of itself...
Avatar of dpapkin

ASKER

ok its great tat my application window is responding perfectly... well abt 0.5s delay but still ok..

but when i look @ the time (the time is compared to my computer time) on my application.. it sometimes it jumps 2 seconds or 3 seconds.

is it normal? or this problem can be solved?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So for the third time I say it:
sending the SMS should be blocking for a separate thread which role would ONLY be to send the message. Another thread would be used to populate a queue with the messages that need to be sent and that are read from the DB that the "send" thread would dequeue. There is no other way to do it since the real issue here is that reading the data from the DB is faster then sending the data through some SMSs.
See my 2 previous comments in which I already explain this !