I am newly join to this forum and I am new with C#. Currenlty i am developing an application in C# as, Each second the application receives data from a sensor wich is attached on RS485 port of the PC. To take data at each second i have used timer with its interval to 1 second. To represent this data i have used Data grid view contol. I am also saving received data in two tables. I found poroblem as after some times it gives StackOverflow error and it hangs. The error comes rendomly as sometimes it appears in 5 hours and sometimes after 1 day. Please help me to solve this problem. I am attaching the code with this.

    private void TimerWsWd_Tick(object sender, EventArgs e)
    {
        try
        {
            Application.DoEvents();
            String St2 = serialPort1.ReadLine();

            //String St3 = serialPort1.ReadExisting();
            //String St2 = "G,171,000.35,K,00,0F";              

            //String[] Rexist = St3.Split('\n');

            //for (int i = 0; i < Rexist.GetUpperBound(0); i++)
            //{
            //String St2 = Rexist[i].ToString();
            bool FlgChsm = CheckSumWsWd(St2);

            if (FlgChsm == false)
                return;

            String[] words = St2.Split(',');

            //listBox1.Items.Add(words[1] + "," + words[2] + "," + words[3]);
            int Sts = 0;
            //int Sts = Validate_WsWd_Data(words[1], words[2], words[3], words[4]);

            DateTime FnlDtm = DateTime.Now;
            DateTime UtM = FnlDtm.ToUniversalTime();

            string FnTm, UnTm;
            FnTm = "";
            UnTm = "";

            if (RbtWRt.Checked == true)
            {
                if (FnlDtm.Minute.ToString().Length == 1 && FnlDtm.Second.ToString().Length == 1)
                    FnTm = FnlDtm.Hour + ":" + "0" + FnlDtm.Minute + ":" + "0" + FnlDtm.Second;
                else if (FnlDtm.Minute.ToString().Length == 2 && FnlDtm.Second.ToString().Length == 1)
                    FnTm = FnlDtm.Hour + ":" + FnlDtm.Minute + ":" + "0" + FnlDtm.Second;
                else if (FnlDtm.Minute.ToString().Length == 1 && FnlDtm.Second.ToString().Length == 2)
                    FnTm = FnlDtm.Hour + ":" + "0" + FnlDtm.Minute + ":" + FnlDtm.Second;
                else if (FnlDtm.Minute.ToString().Length == 2 && FnlDtm.Second.ToString().Length == 2)
                    FnTm = FnlDtm.Hour + ":" + FnlDtm.Minute + ":" + FnlDtm.Second;


                if (UtM.Minute.ToString().Length == 1 && UtM.Second.ToString().Length == 1)
                    UnTm = UtM.Hour + ":" + "0" + UtM.Minute + ":" + "0" + UtM.Second;
                else if (UtM.Minute.ToString().Length == 2 && UtM.Second.ToString().Length == 1)
                    UnTm = UtM.Hour + ":" + UtM.Minute + ":" + "0" + UtM.Second;
                else if (UtM.Minute.ToString().Length == 1 && UtM.Second.ToString().Length == 2)
                    UnTm = UtM.Hour + ":" + "0" + UtM.Minute + ":" + UtM.Second;
                else if (UtM.Minute.ToString().Length == 2 && UtM.Second.ToString().Length == 2)
                    UnTm = UtM.Hour + ":" + UtM.Minute + ":" + UtM.Second;

            }

            String Fstr, Fstr1, Fstr2, Fstr3, Fstr4;

            Fstr1 = IncNo + ",'100','TIFR',";
            Fstr2 = "'" + FnlDtm + "','" + FnlDtm.ToLongDateString() + "','" + FnTm.ToString() + "'," + FnlDtm.Year + "," + FnlDtm.Month + "," + FnlDtm.Day + "," + FnlDtm.Hour + "," + FnlDtm.Minute + "," + FnlDtm.Second + ",";
            Fstr3 = "'" + UtM + "','" + UtM.ToLongDateString() + "','" + UnTm.ToString() + "'," + UtM.Year + "," + UtM.Month + "," + UtM.Day + "," + UtM.Hour + "," + UtM.Minute + "," + UtM.Second + ",";
            Fstr4 = words[1].ToString() + "," + Sts + "," + words[2].ToString() + "," + Sts + ",'" + words[3].ToString() + "'";

            Fstr = Fstr1 + Fstr2 + Fstr3 + Fstr4;

            Conn.Close();
            OdbcCommand Scmd = Conn.CreateCommand();
            Scmd.CommandText = "insert into tbl_WsWd_Data values(" + Fstr + ")";
            Scmd.Connection = Conn;
            Conn.Open();
            Scmd.ExecuteReader();

            Conn.Close();

            OdbcCommand Scmd1 = Conn.CreateCommand();
            Scmd1.CommandText = "insert into tbl_WsWd_Data_rep values(" + Fstr + ")";
            Conn.Open();

            int pn = Scmd1.ExecuteNonQuery();

            Conn.Close();


            //Refresh_WsWd_GridView();
            //DbGrid1.Refresh();
            if (DbGrid1.Rows.Count <= 3600)
            {
                String GrdStr = IncNo + "#" + FnlDtm.ToLongDateString() + "#" + FnlDtm.ToLongTimeString() + "#" + UtM.ToLongDateString() + "#" + UtM.ToLongTimeString() + "#" + words[2].ToString() + "#" + words[1].ToString();
                String[] GrdDta = GrdStr.Split('#');
                DbGrid1.Rows.Add(GrdDta);
                if (DbGrid1.Rows.Count > 6)
                    DbGrid1.FirstDisplayedScrollingRowIndex = DbGrid1.Rows.Count - 5;
                DbGrid1.Rows[DbGrid1.Rows.Count - 1].Selected = true;
            }
            else
            {
                DbGrid1.Rows[GrCnt].Cells[0].Value = IncNo;
                DbGrid1.Rows[GrCnt].Cells[1].Value = FnlDtm.ToLongDateString();
                DbGrid1.Rows[GrCnt].Cells[2].Value = FnlDtm.ToLongTimeString();
                DbGrid1.Rows[GrCnt].Cells[3].Value = UtM.ToLongDateString();
                DbGrid1.Rows[GrCnt].Cells[4].Value = UtM.ToLongTimeString();
                DbGrid1.Rows[GrCnt].Cells[5].Value = words[2].ToString();
                DbGrid1.Rows[GrCnt].Cells[6].Value = words[1].ToString();
                GrCnt = GrCnt + 1;
                if (GrCnt == 3601)
                    GrCnt = 0;
                if (DbGrid1.Rows.Count > 6)
                    DbGrid1.FirstDisplayedScrollingRowIndex = GrCnt - 6;
                else
                    DbGrid1.FirstDisplayedScrollingRowIndex = 0;
                DbGrid1.Rows[GrCnt - 1].Selected = true;
            }

            if (FnlDtm.Hour != WsWd_Pr_Hr)
            {
                IncNo = 0;
                WsWd_Pr_Hr = FnlDtm.Hour;
                waveformGraph3.ClearData();
                waveformGraph4.ClearData();
            }

            IncNo = IncNo + 1;

            String Tmpr, Hum, Prs, Wsp, Wdr, Rfl, Rof, Shn, Stmp, Bvol;
            String Snm = "TIFR";
            String Sid = "100";
            Rof = "---";
            Stmp = "---";
            Bvol = "---";
            Wdr = words[1].ToString();
            Wsp = words[2].ToString();
            Tmpr = "---";
            Hum = "---";
            Prs = "---";
            Rfl = "---";
            Shn = "---";

            waveformGraph3.PlotYAppend(System.Convert.ToDouble(Wsp));
            waveformGraph4.PlotYAppend(System.Convert.ToDouble(Wdr));

            Application.DoEvents();


            //Send_To_DispUnit(Snm, Sid, FnlDtm, Tmpr, Hum, Prs, Wsp, Wdr, Rfl, Rof, Shn, Stmp, Bvol);
            //}
        }
        catch (Exception Ex)
        {
            String Str1 = "Func Name:-TimerWsWd_Tick";
            String Str2 = "Data:-" + Ex.Data.ToString();
            String Str3 = "Msg:-" + Ex.Message.ToString();
            Write_SysLoc(Str1, Str2, Str3);
            Conn.Close();
            return;
        }
    }


    private bool CheckSumWsWd(string Str1)
    {
        try
        {

            //throw new Exception("The method or operation is not implemented.");
            //Str1 = "G,235,002.91,K,00,00";
            //Str1 = "G,171,000.35,K,00,0F";           

            if (Str1.Length < 23)
            //if (Str1.Length < 22)
            {
                String Strx = "Func Name:-CheckSumWsWda";
                String Str2 = "Data:-" + Str1;
                String Str3 = "Msg:-Invalid String Length";
                Write_SysLoc(Strx, Str2, Str3);
                return false;
            }
            String D1 = Str1;
            D1 = D1.Substring(1, 18);

            String L1 = Str1.Substring(Str1.Length - 3, 2);
            int ChsVal = int.Parse(L1, System.Globalization.NumberStyles.HexNumber);

            int X = 0;
            int J = 0;

            for (int i = 0; i < D1.Length; i++)
            {
                if (i == 0)
                    X = System.Convert.ToInt32(System.Convert.ToChar(D1.Substring(i, 1)));
                else if (i == 1)
                    J = X ^ System.Convert.ToInt32(System.Convert.ToChar(D1.Substring(i, 1)));
                else
                    J = J ^ System.Convert.ToInt32(System.Convert.ToChar(D1.Substring(i, 1)));
            }

            if (ChsVal == J)
                return true;
            else
                return false;
        }
        catch (Exception Ex)
        {
            String Strx = "Func Name:-CheckSumWsWda";
            String Str2 = "Data:-" + Ex.Data.ToString();
            String Str3 = "Msg:-" + Ex.Message.ToString();
            Write_SysLoc(Strx, Str2, Str3);
            Conn.Close();
            return false;
        }
    }

hello,

Can you write more about the exception ? line number .. reason error ? variable that gives the error.. so we know what are we looking for ?

most likely the issue is caused by Application.DoEvents();

and the fact it appears more than once in there too, try getting rid of that. If you need to update various controls just tell them to invalidate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.