0

I am fairly new to programming so any help will be taken on board, I am trying to get my CreateTable to run every 2 seconds however when I click my button to execute InitLoop nothing happens, I have tried various different things but haven't been able to get this to work successfully.

    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();
    }
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Jack Holmes
  • 122
  • 6
  • Have you tried setting a breakpoint and stepping through the code line by line? – Bob Horn Aug 31 '13 at 22:16
  • Yes, I am not great at debugging so not really sure what it is doing, I have changed the code to what was specified in one of the answers, however when I debug now it hits the CreateTable 3 times which I think is causing one of the problems, I am new to this so I'm not even sure if using a placeholder to create the table is even the best option, so i'm open to any suggestions. Thanks – Jack Holmes Aug 31 '13 at 22:39

2 Answers2

1

The timer is all you need. The infinite loop is rapidly resetting the timer, so nothing happens.

private void InitLoop(bool runLoop)
{
    try
    {
        myTimer.Elapsed += myTimer_Elapsed;
        myTimer.Interval = 2000;
        myTimer.Enabled = true;
        myTimer.Start();
    }
    catch (Exception f)
    {
        //handle the exception 
    }
}

typically you don't want an infinite loop without some kind of a thread.sleep() command, because it will also drive the CPU to 100% trying to run the loop as fast as possible.

Lathejockey81
  • 1,198
  • 7
  • 8
  • I have tried using this code, however when I am clicking my button it still doesn't display my table? I can get it to display my table when I add System.Threading.Thread.Sleep(5000); in the "try" however it still isn't automatically refreshing my table. – Jack Holmes Aug 31 '13 at 22:37
  • Just checked again, for some reason it is creating to tables now when I press start, not sure if that means anything I can't understand why it is doing that. – Jack Holmes Aug 31 '13 at 22:51
  • Okay, so the button press fires InitLoop, which runs, then passes control back to the page. So if the timer fires before the thread ends, the data is sent to the client. No thread.sleep, no time for the timer to fire. Thread.sleep(5000) means you get two fires of the timer so two tables since you're not deleting the first one from the placeholder before adding the second. You may need to put a javascript timer on the client side to repeatedy fire the createtable method directly. – Lathejockey81 Aug 31 '13 at 23:07
0

The callbacks occur, but most likely the method CreateTable throws an exception. The reason for the exception is that the method tries to change a gui object. In order to change such an object you must be on the gui thread. The thing you need to look up is Dispatcher, and how to dispatch callbacks.

Verify by adding a try-catch to the timer callback method and debug, or write the exception to log file or similar.

erikH
  • 2,286
  • 1
  • 17
  • 19
  • I have added a try-catch to the timer callback method and debugged however it isn't catching anything, when I add a breakpoint to the CreateTable, it is calling this pretty much instantly all the time, I removed the while now so is it ignoring the timer? – Jack Holmes Aug 31 '13 at 22:54