Don't poll ....



  • On any given day I have to suppress urges to post here after I pull changes from a particular colleague (I actually think I could have a chance at out doing snoofle). This WTF along with about 5 others (in 2 days) have finally got me. I recently asked this guy to stop polling the server for updates because the API exposes events if something changes.

    This is how he did that.

      while (true)
                {
                    Connect();
     
                    try
                    {
                        if (IsConnected)
                        { 
                               //      the same polling stuff from before

                               //      Added registration to events

                        }
                    }
                    catch
                    {
                        IsConnected = false;
                    }
     
                    while (IsConnected)                  // These two lines are new
                          Thread.Sleep(1000);
     
     
                    Thread.Sleep(1000);
                }



  • Even if I assume, that the event handlers won't run while the thread is sleeping and that IsConnected won't be automatically updated if the connection breaks, I would still call this a gross misunderstanding of things, but not really a WTF. And neither of those things are obvious. If the socket is handled by some worker thread (your sample does not show whether it's done or not), the events would dispatch and the code would work and the mild laziness of just polling the connected flag instead of setting up an event would not be WTF-worthy.



  • @SandGroper said:

    On any given day I have to suppress urges to post here after I pull changes from a particular colleague (I actually think I could have a chance at out doing snoofle). This WTF along with about 5 others (in 2 days) have finally got me. I recently asked this guy to stop polling the server for updates because the API exposes events if something changes.

    This is how he did that.

      while (true)
                {
                    Connect();
     
                    try
                    {
                        if (IsConnected)
                        { 
                               //      the same polling stuff from before

                               //      Added registration to events

                        }
                    }
                    catch
                    {
                        IsConnected = false;
                    }
     
                    while (IsConnected)                  // These two lines are new
                          Thread.Sleep(1000);
     
     
                    Thread.Sleep(1000);
                }

    Being a complete know-nothing on threading: do those two new lines do what I think they do, i.e. nothing whatsoever, every second, ad infinitum?



  • @toon said:

    Being a complete know-nothing on threading: do those two new lines do what I think they do, i.e. nothing whatsoever, every second, ad infinitum?
    1) Either nothing else (i.e. no other thread) changes IsConnected, in which case yes, it's an infinite loop and does in fact fit the criteria of "stop polling the server for updates."

    1. Something else does update it (possibly one of the events that are now registered,) in which case, every time the connection to the server drops, this snippet tries to open the connection again, every second, until it does connect and polls it. If one of the events deliberately closes the connection, nothing has changed.


  • @toon said:

    @SandGroper said:

    On any given day I have to suppress urges to post here after I pull changes from a particular colleague (I actually think I could have a chance at out doing snoofle). This WTF along with about 5 others (in 2 days) have finally got me. I recently asked this guy to stop polling the server for updates because the API exposes events if something changes.

    This is how he did that.

      while (true)
                {
                    Connect();
     
                    try
                    {
                        if (IsConnected)
                        { 
                               //      the same polling stuff from before

                               //      Added registration to events

                        }
                    }
                    catch
                    {
                        IsConnected = false;
                    }
     
                    while (IsConnected)                  // These two lines are new
                          Thread.Sleep(1000);
     
     
                    Thread.Sleep(1000);
                }

    Being a complete know-nothing on threading: do those two new lines do what I think they do, i.e. nothing whatsoever, every second, ad infinitum?

    It waits until it disconnects, just to reconnect again, and poll again.



  • It registers handlers for the events every time the loop runs. Assuming this is C#, then you'll have the same handler registered to the same event many times and it will only go up each time the loop runs. Eventually, when the event happens the handler will run thousands of times sequentially.



  • @mott555 said:

    It registers handlers for the events every time the loop runs. Assuming this is C#, then you'll have the same handler registered to the same event many times and it will only go up each time the loop runs. Eventually, when the event happens the handler will run thousands of times sequentially.

    Where exactly does that code register event handlers? I'd be very curious to know.

     



  • @TheRider said:

    @mott555 said:

    It registers handlers for the events every time the loop runs. Assuming this is C#, then you'll have the same handler registered to the same event many times and it will only go up each time the loop runs. Eventually, when the event happens the handler will run thousands of times sequentially.

    Where exactly does that code register event handlers? I'd be very curious to know.

    Perhaps here:

                       if (IsConnected)
                        { 
                               //      the same polling stuff from before

                               //      Added registration to events

                        }
    Note the comments



  • If you poll when there are perfectly good events available to subscribe to, you should be beaten with a pole.



  • Hey now, I don't think Mr. Lebowski likes being used as a weapon!



  • @ekolis said:

    Hey now, I don't think Mr. Lebowski likes being used as a weapon!

    Explain thyself.



  • The world "pole" can refer to either a rod, or a person from Poland. Though usually in the latter sense it is capitalized; I just wanted to make a joke :)



  • @ekolis said:

    The world "pole" can refer to either a rod, or a person from Poland. Though usually in the latter sense it is capitalized; I just wanted to make a joke :)
    You are Finished (from Finland) with these puns.



  • @Gazzonyx said:

    @ekolis said:
    The world "pole" can refer to either a rod, or a person from Poland. Though usually in the latter sense it is capitalized; I just wanted to make a joke :)
    You are Finished (from Finland) with these puns.
     

    You guys are turkeys for starting this joke up again...



  • @ekolis said:

    The world "pole" can refer to either a rod, or a person from Poland.

    It can also be a euphemism for penis or a length of 16.5 feet. Leading to this delightful sentence:

    Pole1 pole2 pole3 poll4 Pole1 pole2 pole3.

    1 A Pollack.

    2 16.5 feet

    3 A penis.

    4 Takes a sample of.



  • @mott555 said:

    @Gazzonyx said:

    @ekolis said:
    The world "pole" can refer to either a rod, or a person from Poland. Though usually in the latter sense it is capitalized; I just wanted to make a joke :)
    You are Finished (from Finland) with these puns.
     

    You guys are turkeys for starting this joke up again...

    That's what France are for.



  • @morbiuswilters said:

    Pole1 pole2 pole3 poll4 Pole1 pole2 pole3.

    1 A Pollack.

    2 16.5 feet

    3 A penis.

    4 Takes a sample of.

    I can't parse this. If the 3rd "pole" is singular, why is "poll" not "polls"? If the 6th "pole" is singular, why isn't there an article?



  • @Spectre said:

    @morbiuswilters said:
    Pole1 pole2 pole3 poll4 Pole1 pole2 pole3.

    1 A Pollack.

    2 16.5 feet

    3 A penis.

    4 Takes a sample of.

    I can't parse this. If the 3rd "pole" is singular, why is "poll" not "polls"? If the 6th "pole" is singular, why isn't there an article?

    You really suck at jokes and I hate you.



  • @morbiuswilters said:

    You really suck at jokes and I hate you.
     

    I appreciate the effort.



  • @morbiuswilters said:

    @Spectre said:
    @morbiuswilters said:
    Pole1 pole2 pole3 poll4 Pole1 pole2 pole3.

    1 A Pollack.

    2 16.5 feet

    3 A penis.

    4 Takes a sample of.

    I can't parse this. If the 3rd "pole" is singular, why is "poll" not "polls"? If the 6th "pole" is singular, why isn't there an article?

    You really suck at jokes and I hate you.

    NO U


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.