I have a winforms application, which draws some controls inside a panel, obtaining some data from a database, and running in an endless while cycle with a time of 5 seconds between each query. the form is drawn correctly, and the data also matches those in the database. The problem is that the click that runs infinitely, does not refresh the data when the value in the database changes. If for example I stop the application, and restart it, the data is displayed correctly as they are in the database. here is my code of the form.
private void button1_Click(object sender, EventArgs e)
{
while (1 == 1)
{
int iContador = 0;
CA_DispensadorRepository dispensadorRepo = new CA_DispensadorRepository();
List<CA_Dispensador> lstDispensador = dispensadorRepo.GetAll();
if (lstDispensador != null && lstDispensador.Count > 0)
{
foreach (CA_Dispensador dispensador in lstDispensador)
{
Application.DoEvents();
iContador++;
if (iContador <= 5)
{
GroupBox MiGroupBox = new System.Windows.Forms.GroupBox();
MiGroupBox.Name = dispensador.DirecionHexa.Trim().ToUpper();
Label AbiertoLabel = new System.Windows.Forms.Label();
if (dispensador.Abierto)
{
AbiertoLabel.Text = "Abierto SI";
}
else
{
AbiertoLabel.Text = "Abierto NO";
}
AbiertoLabel.Size = new System.Drawing.Size(80, 17);
AbiertoLabel.AutoSize = true;
AbiertoLabel.Location = new System.Drawing.Point(17, 20);
AbiertoLabel.Name = "Abierto";
AbiertoLabel.TabIndex = 0;
MiGroupBox.Controls.Add(AbiertoLabel);
int Columna = 12 * (iContador * 6);
MiGroupBox.Location = new Point(12, Columna);
MiGroupBox.SuspendLayout();
MiGroupBox.Size = new System.Drawing.Size(200, 71);
MiGroupBox.TabIndex = 0;
MiGroupBox.TabStop = false;
panel1.Controls.Add(MiGroupBox);
Application.DoEvents();
}
Application.DoEvents();
}
}
Application.DoEvents();
Thread.Sleep(5000);
}
}
Any idea why the application is "frozen" ?? Greetings and thanks for your time