0

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.

user3505003
  • 45
  • 1
  • 2
  • 7
  • You have an infinite loop. Nothing is going to update your flag. Each postback, whether it's an async postback or not, will result in a new creation of your page class, and thus your flag variable is not in the correct instance to get updated. – mason Jan 20 '17 at 18:49
  • Actually, you have two infinite loops. `while(true)` will never get broken out of by itself, you have to explicitly call `break;` – mason Jan 20 '17 at 18:52
  • I don't need to break out of `while(true)` because I need to iterate through the list for as long as the page is open. – user3505003 Jan 20 '17 at 18:54
  • That's not how the web works. If you do that, the page will execute forever and never return a response, at least until it times out. Classes don't stick around while a page is open. HTTP request comes in, ASP.NET creates an instance of the page, grabs the response, sends it to the user, and that instance of the page *goes away*. – mason Jan 20 '17 at 18:56
  • @mason Ok that's great, but could you suggest a different approach to tackling this problem that would work? – user3505003 Jan 20 '17 at 23:37
  • Rewrite your code so it doesn't have infinite loops. Better to avoid UpdatePanel and timers too. Why not use JavaScript to poll a web service? – mason Jan 20 '17 at 23:43
  • I thought of JS, but wanted to see if it could be accomplished just with c#. – user3505003 Jan 21 '17 at 00:04
  • @mason why should UpdatePanel and timers be avoided? – user3505003 Jan 23 '17 at 21:12
  • Because they're a terrible abstraction that lead to needless complexity and inefficient code. Actually learning how the web works and avoiding those will lead to applications that are easier to write and debug, and run faster. – mason Jan 23 '17 at 21:14
  • See [this question](http://stackoverflow.com/questions/25829343/how-to-implement-real-time-data-for-a-web-page) I put together a while back that compares several different ways of doing this. – mason Jan 23 '17 at 21:17

1 Answers1

0

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.

user3505003
  • 45
  • 1
  • 2
  • 7