Run minute to minute routine with C#

Asked

Viewed 1,093 times

1

Good morning, I have a problem that I have not been able to solve, I have a routine that will monitor an equipment every second and when it turns the minute the system will perform the desired action, the routine needs to read every second because the minute value will be provided by the equipment(a PLC Allenbradley). When it turns the minute, I do the entire collection process, but it is repeating several times the process generating duplicity. As I am using Windows Forms, I put the time to be counted in a Timer component of Windows Forms. As soon as the minute turns, it activates my function as it should be only that it repeats the same action several times, already tried to put a Sleep, tried to stop the timer(timer1.stop) and nothing solved. Can someone give me a light? Grateful.

Follows code timer and timer_tick

private void InitializeTimer(string tTimer, string ipPLC)
    {
        try
        {

            ethernet.IPAddress = ipPLC;
            timeHour = Convert.ToInt16(ethernet.Read("HORA[4]"));// pega o minuto para comparação

            timer1.Interval = Convert.ToInt16(tTimer);
            timer1.Tick += new EventHandler(timer1_Tick);

            //Habilita o timer
            timer1.Enabled = true;


        }
        catch (Exception e)
        {

            MessageBox.Show(e.ToString());
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        try
        {
            if (timeHour != Convert.ToInt16(ethernet.Read("HORA[4]")))
            {

                timeHour = Convert.ToInt16(ethernet.Read("HORA[4]"));

                if (ethernet.Read("HORA[4]").Length < 2)
                {
                    minuto = "0" + ethernet.Read("HORA[4]");
                }else
                    minuto = ethernet.Read("HORA[4]");

                tasgPojoList = new List<TagsPojo>();

                tagsPojo = new TagsPojo();
                tagsPojo.Id_Tag = 1;//fixo para testes
                tagsPojo.ValueTag = float.Parse(ethernet.Read("Program:Etanol3.FT141001_xTot"), System.Globalization.CultureInfo.InvariantCulture);
                tagsPojo.Hour = ethernet.Read("HORA[3]") + ":" + minuto;
                tasgPojoList.Add(tagsPojo);

                tagsPojo = new TagsPojo();
                tagsPojo.Id_Tag = 2;//fixo para testes
                tagsPojo.ValueTag = float.Parse(ethernet.Read("Program:Etanol0.FT141002_xTot"), System.Globalization.CultureInfo.InvariantCulture);
                tagsPojo.Hour = ethernet.Read("HORA[3]") + ":" + minuto;
                tasgPojoList.Add(tagsPojo);


               CadTagController cadTagController = new CadTagController();
               cadTagController.CadTag(tasgPojoList);

               System.Threading.Thread.Sleep(100);                    

            }


            label21.Text = "Hora: " + ethernet.Read("HORA[3]");
            label22.Text = "Minutos: " + ethernet.Read("HORA[4]");

            label23.Text = "Segundos: " + ethernet.Read("HORA[5]");

        }
        catch (Exception EF)
        {

           // MessageBox.Show(EF.ToString());
        }
    }

BD-> data insertion duplicating here

public void InsertTagH(List<TagsPojo> tags)
    {
        try
        {

            StringBuilder sqlCreateDBQuery1 = new StringBuilder();

            tmpConn = new SqlConnection();
            tmpConn.ConnectionString = ConfigurationManager.ConnectionStrings["dbConnString"].ConnectionString;

            DateTime dataNow = DateTime.Now;
            string formataData = dataNow.ToString();
            formataData = formataData.Substring(0, 10);

            foreach (TagsPojo tagsPojo in tags)
            {
                tmpConn.Open();                    

                sqlCreateDBQuery1.Append("INSERT INTO ColetaH(Value, Data, Hour, Id_tag) VALUES(@Value, @Data, @Hour, @Id_tag);");

                SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery1.ToString(), tmpConn);

                myCommand.Parameters.AddWithValue("@Value", tagsPojo.ValueTag);
                myCommand.Parameters.AddWithValue("@Data", FData(formataData));
                myCommand.Parameters.AddWithValue("@Hour", tagsPojo.Hour);
                myCommand.Parameters.AddWithValue("@Id_tag", tagsPojo.Id_Tag);                    

                myCommand.ExecuteNonQuery();

                tmpConn.Close();

                //System.Threading.Thread.Sleep(100);
            }


        }
        catch (SqlException e)
        {
            throw e;
        }
        catch (Exception e)
        {

            throw e;
        }
    }
  • You could add the code of how you are doing so far?

  • 1

    See if it helps you: http://answall.com/q/81084/101 and http://answall.com/q/30601/101

  • I don’t know if this would help you, but in the Try...catch block, put a Finally block and "zere" all variables, connections and etc.... This ensures in the next minute have no information.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.