Search code examples
c#asp.netdotnetnukebusy-waiting

Busy waiting in codebehind - dotnetnuke


I'm writing a small module for a DNN website. I need to update a <div> every n seconds with the contents of a list from the codebehind, one list item at a time. I set up an <asp:UpdatePanel> on the webpage along with a 5 second <asp:Timer>. In my OnTick() I set a global variable flag to 1. Then I busy wait in a while loop, and everytime flag changes from 0 to 1, I update the <div>'s contents:

List<String> dcList1 = new List<string>();
    for (int i = 0; i < 10; i++)
        dcList1.Add(i.ToString());
            while (true)
            {
                for (int i = 0; i < dcList1.Count; i++)
                {
                   //busy wait
                   while (flag == 0)
                   {
                       ;
                   }
                   mainDiv.InnerHtml = dcList1[i].ToString();
                   //reset flag
                   flag = 0;
               }
           }

The page ends up hanging for a while and eventually DNN throws a timeout error. I'm trying to understand why using a global variable flag like that doesn't work. Thank s a lot.

P.S. My timer is set up properly as the <div> gets updated if I get rid the while loops and just update it with DateTime.Now.


Solution

  • Ok I just read that server side in ASP.net is stateless, so flag in my OnTick and the other method come from separate instances of the class. I ended up storing my data in cookies and then using them to periodically update the div.