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.

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.

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?

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."

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.

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.

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.

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.