我对编程相当陌生,因此将获得任何帮助,我试图让我的 CreateTable 每 2 秒运行一次,但是当我单击按钮执行 InitLoop 时没有任何反应,我尝试了各种不同的方法,但还没有能够使它成功工作。
private void CreateTable()
{
//Set the number of columns and rows
int tblColumns = 20;
int tblRows = 50;
//Create the table
Table tbl = new Table();
tbl.CssClass = "table";
//Add table
PlaceHolder1.Controls.Add(tbl);
Random RandomNumber = new Random();
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblColumns; j++)
{
TableCell tc = new TableCell();
int Range = RandomNumber.Next(1, 99);
tc.Text = Range.ToString();
//Add Columns
tr.Cells.Add(tc);
}
//Add Rows
tbl.Rows.Add(tr);
}
return;
}
System.Timers.Timer myTimer = new System.Timers.Timer();
private void InitLoop(bool runLoop)
{
while (true)
{
try
{
myTimer.Elapsed += myTimer_Elapsed;
myTimer.Interval = 2000;
myTimer.Enabled = true;
myTimer.Start();
}
catch (Exception f)
{
//handle the exception
}
}
}
private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
CreateTable();
}