title: C# My form objects don't work properly after application running a while
body: <p>I have 2 pictureboxes and a couple of labels and buttons in my form. The application reads variables from a PLC to draw a graph on pictureboxes and checks if any alarm has been occured by reading variables from PLC. I have 3 timers working at 0.5, 1 and 30 seconds interval for each one. After a while, pictureboxes go invalid. I mean red rectangle on white background by invalid. Buttons don't work. I run application from exe built by debug mode. No errors or exceptions have been occured. Should i refresh my form by my code? Should i enable double buffering property? Or what else should i do? Thanks for your time.</p>
  
  <p>Best regards,
  Bee-</p>
  
  <p>Note: Before some changes the app worked around a week. And it was all ok. Changes are: 2 timers added(0.5 and 1 sec interval) and added a picturebox.</p>
  
  <p>Edit: When i go debug mode, i figured out that picturebox.Invalidate() commands doesn't refresh picturebox. After invalidate command, debugger didn't hit paint event method. And also buttons creates form object but can't show it. I think there is something wrong with graphics/rendering. I'm just a beginner and thanks for your help. Codes below: </p>
  
  <p>Reading values to create graph and invalidate pictureboxes:</p>
  
  <pre><code> private void timerCommunication_Tick(object sender, EventArgs e)
          {
              CurrentActiveShift();
              if (Global_Variables.isShiftActiveNow)
              {
  
                  if (adsClient.IsConnected == false)
                      adsClient.Connect(801);
                  string info = adsClient.ReadDeviceInfo().ToString();
                  float ActData = (float)adsClient.ReadAny(16416, 200, typeof(float));
                  textBox1.Text = Convert.ToString(ActData);
  
                  string[] vs = new string[9];
                  string[] vs1 = new string[6];
                  if (DateTime.Now.Hour &lt; 10)
                      vs[0] = "0" + DateTime.Now.Hour.ToString() + ":";
                  else
                      vs[0] = DateTime.Now.Hour.ToString() + ":";
  
                  if (DateTime.Now.Minute &lt; 10)
                      vs[0] += "0" + DateTime.Now.Minute.ToString();
                  else
                      vs[0] += DateTime.Now.Minute.ToString();
  
                  vs[1] = Convert.ToString(ActData);
                  vs1[0] = vs[0];
                  ActData = (float)adsClient.ReadAny(16416, 216, typeof(float));
                  vs1[1] = Convert.ToString(ActData);
  
                  float PrmUpperLimit = Global_Variables.PrmSetValue + Global_Variables.PrmAlarm + Global_Variables.PrmWarning + Global_Variables.PrmMaxMin;
                  float PrmLowerLimit = Global_Variables.PrmSetValue - Global_Variables.PrmAlarm - Global_Variables.PrmWarning - Global_Variables.PrmMaxMin;
                  float PrmSet = Global_Variables.PrmSetValue;
                  float PrmUpperWarning = Global_Variables.PrmSetValue + Global_Variables.PrmWarning;
                  float PrmLowerWarning = Global_Variables.PrmSetValue - Global_Variables.PrmWarning;
                  float PrmUpperAlarm = PrmUpperWarning + Global_Variables.PrmAlarm;
                  float PrmLowerAlarm = PrmLowerWarning - Global_Variables.PrmAlarm;
  
                  float OvlUpperLimit = Global_Variables.OvlSetValue + Global_Variables.OvlAlarm + Global_Variables.OvlWarning + Global_Variables.OvlMaxMin;
                  float OvlSet = Global_Variables.OvlSetValue;
                  float OvlUpperWarning = Global_Variables.OvlSetValue + Global_Variables.OvlWarning;
                  float OvlUpperAlarm = OvlUpperWarning + Global_Variables.OvlAlarm;
  
  
                  vs[2] = Convert.ToString(PrmUpperLimit);
                  vs1[2] = Convert.ToString(OvlUpperLimit);
                  vs[3] = Convert.ToString(PrmUpperAlarm);
                  vs1[3] = Convert.ToString(OvlUpperAlarm);
                  vs[4] = Convert.ToString(PrmUpperWarning);
                  vs1[4] = Convert.ToString(OvlUpperWarning);
                  vs[5] = Convert.ToString(PrmSet);
                  vs1[5] = Convert.ToString(OvlSet);
                  vs[6] = Convert.ToString(PrmLowerWarning);
                  vs[7] = Convert.ToString(PrmLowerAlarm);
                  vs[8] = Convert.ToString(PrmLowerLimit);
  
                  //string Header = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString() + "," + TimeSpan.FromMinutes(Global_Variables.CurrentShiftStart).ToString(@"hh\.mm") + "-" + TimeSpan.FromMinutes(Global_Variables.CurrentShiftEnd).ToString(@"hh\.mm");
                  string Header = DateTime.Now.ToString("yyyy.MM.dd") + "," + TimeSpan.FromMinutes(Global_Variables.CurrentShiftStart).ToString(@"hh\.mm") + "-" + TimeSpan.FromMinutes(Global_Variables.CurrentShiftEnd).ToString(@"hh\.mm");
                  //string Dir = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString();
                  string Dir = DateTime.Now.ToString("yyyy.MM.dd");
                  System.IO.Directory.CreateDirectory(Application.StartupPath + "\\Archive\\" + Dir);
                  MeasuringLog.WriteLog(Application.StartupPath + "\\Archive\\" + Dir + "\\" + Header + ".txt", vs);
                  MeasuringLog.WriteLog(Application.StartupPath + "\\Archive\\" + Dir + "\\" + Header + ".txt", vs1);
  
                  picboxPerimeter.Invalidate();
                  picboxOvality.Invalidate();
  }
  </code></pre>
  
  <p>Picturebox Paint Event:</p>
  
  <pre><code> private void picboxPerimeter_Paint(object sender, PaintEventArgs e)
          {
              CurrentActiveShift();
              if (Global_Variables.isShiftActiveNow)
              {
  
                  string[] X = new string[2];
                  //string Header = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString() + "," + TimeSpan.FromMinutes(Global_Variables.CurrentShiftStart).ToString(@"hh\.mm") + "-" + TimeSpan.FromMinutes(Global_Variables.CurrentShiftEnd).ToString(@"hh\.mm");
                  string Header = DateTime.Now.ToString("yyyy.MM.dd") + "," + TimeSpan.FromMinutes(Global_Variables.CurrentShiftStart).ToString(@"hh\.mm") + "-" + TimeSpan.FromMinutes(Global_Variables.CurrentShiftEnd).ToString(@"hh\.mm");
                  //string Dir = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString();
                  string Dir = DateTime.Now.ToString("yyyy.MM.dd");
                  string path = Application.StartupPath + "\\Archive\\" + Dir + "\\" + Header + ".txt";
                  try
                  {
                      int index;
                      string[] ReadLog = System.IO.File.ReadAllLines(path);
  
                      float LargestSet = LargestLimit(ReadLog, 5, 0, out index);
                      float LargestTrend = LargestLimit(ReadLog, 2, 0, out index);
                      string[] FillLbL = ReadLog[index].Split('|');
                      string[] myL = ReadLog[index].Split('|');
                      FillLabelsPrm(FillLbL);
                      Trend.SetTrendLimits(LargestTrend, float.Parse(myL[8]));
                      listPerimeter.Clear();
  
                      X[0] = TimeSpan.FromMinutes(Global_Variables.CurrentShiftStart).ToString(@"hh\:mm");
                      X[1] = TimeSpan.FromMinutes(Global_Variables.CurrentShiftEnd).ToString(@"hh\:mm");
                      Trend.Time = X;
                      string[] unravel = ReadLog[0].Split('|');
                      Trend.p1 = unravel;
  
                      for (int i = 2; i &lt;= ReadLog.Length - 1; i += 2)
                      {
                          unravel = ReadLog[i].Split('|');
                          bool Check = Trend.DrawWholePicture(unravel, sender, e, listPerimeter);
                      }
  
                  }
                  catch
                  {
                      ;
                  }
  
                  Point L_ptc = new Point();
                  L_ptc = picboxPerimeter.PointToClient(new Point(0, 0));
                  L_ptc = new Point(-L_ptc.X, -L_ptc.Y + picboxPerimeter.Size.Height);
                  Pen p = new Pen(Color.Black, 1.5f);
                  Label[] labels = new Label[17];
                  for (int i = 0; i &lt;= 16; i++)
                  {
                      e.Graphics.DrawLine(p, i * picboxPerimeter.Width / 16, 0, i * picboxPerimeter.Width / 16, picboxPerimeter.Height);
  
                      labels[i] = new Label();
                      this.Controls.Add(labels[i]);
                      labels[i].Location = new Point(L_ptc.X - 15 + i * picboxPerimeter.Width / 16, L_ptc.Y - 15);
                      labels[i].BackColor = Color.Transparent;
                      labels[i].Size = new Size(35, 15);
                      labels[i].BringToFront();
  
                      try
                      {
                          double archive_sh = Convert.ToDouble(X[0].Substring(0, 2)) * 60 + Convert.ToDouble(X[0].Substring(3));
                          double archive_sh1 = Convert.ToDouble(X[1].Substring(0, 2)) * 60 + Convert.ToDouble(X[1].Substring(3));
                          if (archive_sh1 &lt; archive_sh)
                              archive_sh1 += 24 * 60;
                          double a = archive_sh1 - archive_sh;
                          a /= 16.00;
                          double b = archive_sh;
  
                          TimeSpan span = TimeSpan.FromMinutes(b + i * a);
                          labels[i].Text = span.ToString(@"hh\:mm");
                      }
                      catch
                      {
                          ;
                      }
                  }
              }
          }
  </code></pre>
  
  <p>The other picturebox' event is the same, the difference is: this one draw graph according to odd numbered lines of txt file and the other draws according to even numbered lines. Adding other timers' tick event methods below, don't know if necessary though. </p>
  
  <p>Timer1; 1 sec interval, checks alarm values from PLC:</p>
  
  <pre><code> private void timer1_Tick(object sender, EventArgs e)
          {
              lblNotification.Text = "";
  
              if (adsClient.IsConnected == false)
                  adsClient.Connect(801);
              if((bool)adsClient.ReadAny(16417, 1, typeof(bool)))
              {
                  adsClient.WriteAny(16417, 1, false);
              }
              if ((bool)adsClient.ReadAny(16417, 2, typeof(bool)))
              {
                  adsClient.WriteAny(16417, 2, false);
              }
              bool alarm = (bool)adsClient.ReadAny(16416, 1, typeof(bool));
              bool alarm1 = (bool)adsClient.ReadAny(16416, 2, typeof(bool));
              bool[] myBool = (bool[])adsClient.ReadAny(16416, 400, typeof(bool[]), new int[] { 14 });
  
              //string alarmDir = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString();
              string alarmDir = DateTime.Now.ToString("yyyy.MM.dd");
              System.IO.Directory.CreateDirectory(Application.StartupPath + "\\Alarm\\" + alarmDir);
              //string alarmPath = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString() + "," + TimeSpan.FromMinutes(Global_Variables.CurrentShiftStart).ToString(@"hh\.mm") + "-" + TimeSpan.FromMinutes(Global_Variables.CurrentShiftEnd).ToString(@"hh\.mm");
              string alarmPath = DateTime.Now.ToString("yyyy.MM.dd") + "," + TimeSpan.FromMinutes(Global_Variables.CurrentShiftStart).ToString(@"hh\.mm") + "-" + TimeSpan.FromMinutes(Global_Variables.CurrentShiftEnd).ToString(@"hh\.mm");
              int alarmCount = 0;
  
              for (int i=0; i &lt;= myBool.Length - 1; i++)
              {
                  alarmMessage[i] = AlarmLog.GetMessage(i);
                  if (!alarmisActive[i] &amp;&amp; myBool[i])
                  {
                      alarmisActive[i] = true;
                      alarmActiveTime[i] = DateTime.Now.ToString("HH:mm");
                      alarmCount++;
                      if (!alarmisWritten[i])
                      {
                          lblNotification.Text = alarmMessage[i];
                          alarmisWritten[i] = true;
                          alarmFileData = alarmMessage[i] + "|" + alarmActiveTime[i] + "|" + alarmisActive[i].ToString() + "|" + alarmDeactiveTime[i] + "\r\n";
                          System.IO.File.AppendAllText(Application.StartupPath + "\\Alarm\\" + alarmDir + "\\" + alarmPath + ".txt", alarmFileData);
                      }
                  }
                  else if(alarmisActive[i] &amp;&amp; !myBool[i])
                  {
                      if (alarmisWritten[i])
                      {
                          alarmisActive[i] = false;
                          alarmDeactiveTime[i] = DateTime.Now.ToString("HH:mm");   
                          alarmisWritten[i] = false;
                          alarmFileData = alarmMessage[i] + "|" + alarmActiveTime[i] + "|" + alarmisActive[i].ToString() + "|" + alarmDeactiveTime[i] + "\r\n";
                          System.IO.File.AppendAllText(Application.StartupPath + "\\Alarm\\" + alarmDir + "\\" + alarmPath + ".txt", alarmFileData);
                      }
                  }
  
              }
  
              bool etherCatAlarm = (bool)adsClient.ReadAny(16417, 7, typeof(bool));
              alarmMessage[14] = AlarmLog.GetMessage(14);
              if (!alarmisActive[14] &amp;&amp; etherCatAlarm)
              {
                  alarmisActive[14] = true;
                  alarmActiveTime[14]= DateTime.Now.ToString("HH:mm");
                  alarmCount++;
                  if (!alarmisWritten[14])
                  {
                      alarmisWritten[14] = true;
                      alarmFileData = alarmMessage[14]+ "|" + alarmActiveTime[14] + "|" + alarmisActive[14].ToString() + "|" + alarmDeactiveTime[14] + "\r\n";
                      System.IO.File.AppendAllText(Application.StartupPath + "\\Alarm\\" + alarmDir + "\\" + alarmPath + ".txt", alarmFileData);
                  }
              }
              else if (alarmisActive[14] &amp;&amp; !etherCatAlarm)
              {
                  if (alarmisWritten[14])
                  {
                      alarmisActive[14] = false;
                      alarmDeactiveTime[14] = DateTime.Now.ToString("HH:mm");
                      alarmisWritten[14] = false;
                      alarmFileData = alarmMessage[14] + "|" + alarmActiveTime[14] + "|" + alarmisActive[14].ToString() + "|" + alarmDeactiveTime[14] + "\r\n";
                      System.IO.File.AppendAllText(Application.StartupPath + "\\Alarm\\" + alarmDir + "\\" + alarmPath + ".txt", alarmFileData);
                  }
              }
  
              if(alarmCount &gt; 1)
              {
                  lblNotification.Text = "Multiple alarms are active, check \"Active Alarm\" page...";
              }
              else if (alarmCount == 1)
              {
                  for (int i = 0; i &lt;= 13; i++)
                  {
                      lblNotification.Text = alarmMessage[i];
                  }
              }
          }
  </code></pre>
  
  <p>Timer2; 0.5 sec interval, reads values to show in labels:</p>
  
  <pre><code>private void timer2_Tick(object sender, EventArgs e)
          {
              if (adsClient.IsConnected == false)
                  adsClient.Connect(801);
              float[] Actual = (float[])adsClient.ReadAny(16416, 200, typeof(float[]), new int[] { 8 });
              lblPerimeterValue.Text = Actual[0].ToString();
              lblPerimeterX.Text = Actual[1].ToString();
              lblPerimeterY.Text = Actual[2].ToString();
              lblOvalityValue.Text = Actual[4].ToString();
              lblOvalityX.Text = Actual[5].ToString();
              lblOvalityY.Text = Actual[6].ToString();
          }
  </code></pre>