Good morning, I have a problem that I still can not solve, I have a routine that will monitor an equipment every second and the minute the system will perform the desired action, the routine must read every second because the value the equipment (an AllenBradley PLC). When you turn the minute, I do the entire collection process, but it is repeating the process several times causing duplicity. As I'm using Windows Forms, I put the time to be counted in a Windows Forms Timer component. As soon as he sees the minute, he activates my function as it should only be that he repeats the same action several times, I tried to put a sleep, tried to stop the timer (timer1.stop) and nothing solved. Can someone give me a light? Thankful.
Follow 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());
}
}
Insert BD-> data 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;
}
}